I want to audit insertions, updates, deletions, etc using entitymanager. For this, how could I do an interceptor for EntityManager.class that will work with EJB???
I want to audit insertions, updates, deletions, etc using entitymanager. For this, how could
Share
You don’t need to add an interceptor for that, simply use JPA’s callback methods and/or entity listeners.
With the first approach, you add to an entity methods declared with one of these annotations:
@PrePersist,@PostPersist,@PreUpdate,@PostUpdate,@PreRemove,@PostRemove, or@PostLoad. The names are self-explanatory, meaning that for each event (pre-persist, post-persist, etc.) the annotated method gets called.The second approach is similar, but the methods are implemented in one or more separate classes, which in turn are added to the entity using the
@EntityListenersannotation.The second approach is more flexible, but either way you can intercept persistence operation right before/after they occur and perform the operations you need.