tags:

views:

522

answers:

1

I want to have a function in one of my JPA annotated classes that is called every time the EntityManager saves them to the database (aka event listener). Is that possible? Preferably, I'd like to distinguish whether it is just about to be saved or has just been saved.

To accomplish what I want, this time I could probably work around with a setter method, but it wouldn't be quite the same.

+2  A: 

Sure, check out these examples. You have:

  • @PrePersist;
  • @PreUpdate;
  • @PreRemove.
  • @PostPersist;
  • @PostUpdate; and
  • @PostRemove.

(added by Hanno) - Using this answer for starters, I also found this other nice example tutorial.

cletus