我如何为EntityManager.class做一个拦截器 [英] How could I do an interceptor for EntityManager.class

查看:133
本文介绍了我如何为EntityManager.class做一个拦截器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用entitymanager审核插入,更新,删除等。为此,我该怎么做一个EntityManager.class的拦截器,它将与EJB一起工作?

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???

推荐答案

你不需要添加拦截器,只需使用JPA的回调方法和/或实体侦听器

You don't need to add an interceptor for that, simply use JPA's callback methods and/or entity listeners.

使用第一种方法,您可以添加使用以下注释之一声明的实体方法: @PrePersist @PostPersist @PreUpdate @PostUpdate @PreRemove @PostRemove @PostLoad 。这些名称是不言而喻的,这意味着对于每个事件(预持续,后持续等),注释的方法被调用。

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.

第二种方法是类似的,但是这些方法是在一个或多个单独的类中实现的,而这些类又被添加到使用 @EntityListeners 注释的实体中。

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.

这篇关于我如何为EntityManager.class做一个拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆