tags:

views:

27

answers:

2

I am somewhat new to using JPA -- I'll put that out there right off the bat. I'm getting more familiar with it, but there are big holes in my knowledge right now.

I am working on an application that uses JPA, and deletes entities using the EntityManager.remove(Object entity) function. However, the application also links into a third-party application, and I would like to add logic that gets executed whenever a certain type of Entity is removed from the persistence layer.

My question is this. Is there a way to add logic to the EntityManager.remove(Object entity) function on a Entity class level, such that every time that type of entity is deleted the extra logic is executed?

Thanks much.

+3  A: 

Entity class may have methods annotated with @PreRemove or @PostRemove.

axtavt
I'm looking into those, and those look like what I'm looking for. Looks like the PostRemove gets called closer to the point of commit. While I see that it doesn't guarantee that the database entity is actually deleted, it's at least not as prone to be affected by transaction rollback. Is that correct?
Dante617
So far I remember, @PreRemove will always be called inside the tx, but @PostRemove can be called inside or outside the tx, depending when the flush happens, which will at least be before the commit, but can also happen within a tx. I suspect you should do the work in PreRemove and make sure what you do is ok from point of view of tx.
ewernli
A: 

If you are using Eclipselink, it has a much more fine grained native event system via the DescriptorEventListener interface.

Tbee
I am actually using TopLink, but with everything I keep finding out about Eclipselink, it's looking like the more full-featured relative.
Dante617