How could I do an interceptor for EntityManager.class

风格不统一 提交于 2019-12-10 23:56:45

问题


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.



来源:https://stackoverflow.com/questions/9294439/how-could-i-do-an-interceptor-for-entitymanager-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!