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???
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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 @EntityListeners
annotation.
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.