java 在 JPA 环境中声明 Hibernate 事件侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2363236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring Hibernate event listeners in a JPA environment
提问by user284400
Hy guys,
嘿伙计们,
I am working on a project developed in Java EE 5 environment. I want to know how I can declare a Hibernate event listener so that I can be informed when CRUD operation are performed.
我正在开发一个在 Java EE 5 环境中开发的项目。我想知道如何声明 Hibernate 事件侦听器,以便在执行 CRUD 操作时通知我。
I've read that I must declare in the Hibernate configuration file *cfg.xmlsomething like this:
我读过我必须在 Hibernate 配置文件中声明*cfg.xml如下内容:
<hibernate-configuration>
<session-factory>
...
<event type="load">
<listener class="com.eg.MyLoadListener"/>
<listener class="org.hibernate.event.def.DefaultLoadEventListener"/>
</event>
</session-factory>
</hibernate-configuration>
The problem is I don't have such a file in the project. We are using JPA (with Hibernate as the underlying implementation). Do you know if I need to create that specific file? If yes where should I put it?
问题是我在项目中没有这样的文件。我们正在使用 JPA(以 Hibernate 作为底层实现)。你知道我是否需要创建那个特定的文件吗?如果是,我应该把它放在哪里?
Thanks in advance.
提前致谢。
回答by mtpettyp
In your persistence.xml:
在您的persistence.xml 中:
<persistence>
<persistence-unit name="myPersistenceUnit">
...
<snip/>
...
<properties>
<property name="hibernate.ejb.event.load" value="com.eg.MyLoadListener,org.hibernate.event.def.DefaultLoadEventListener"/>
</properties>
</persistence-unit>
</persistence>
In the Hibernate EntityManager docslook at "Table 2.1. Hibernate Entity Manager specific properties" for all applicable properties.
在Hibernate EntityManager 文档中,查看所有适用属性的“表 2.1. Hibernate Entity Manager 特定属性”。
回答by Henning
I assume you are using annotations? If so, you can use the @EntityListenersannotation to do that, like so:
我假设您正在使用注释?如果是这样,您可以使用@EntityListeners注释来做到这一点,如下所示:
@MappedSuperclass
@EntityListeners(AbstractEntityListener.class)
public abstract class AbstractEntity {
...
}
Your entity listener class could look like this:
您的实体侦听器类可能如下所示:
public class AbstractEntityListener {
/**
* Set creation and lastUpdated date of the entity.
*
* @param entity {@link AbstractEntity}
*/
@PrePersist
@PreUpdate
public void setDate(final AbstractEntity entity) {
final Date now = new Date();
entity.setModified(now);
}
}
There are different annotations available to catch different events, like @PrePersist, @PreUpdate, @PostLoad, etc.
有可捕获不同的事件不同的注解,如@PrePersist,@PreUpdate,@PostLoad,等。
回答by ewernli
Note that you can also specify this with annotations on the callback methods. Either embed them in the entity itself, or in a separate class, called an entity listener. Here is a snippet taken from the documentation:
请注意,您还可以使用回调方法的注释来指定这一点。要么将它们嵌入实体本身,要么嵌入一个单独的类,称为实体侦听器。这是从文档中截取的片段:
@Entity
@EntityListeners(class=Audit.class)
public class Cat {
@Id private Integer id;
private String name;
@PostLoad
public void calculateAge() {
...
}
}
public class LastUpdateListener {
@PreUpdate
@PrePersist
public void setLastUpdate(Cat o) {
...
}
}
I guess you can also specify that in the XML config. But annotation are more convenient in my opinion.
我想您也可以在 XML 配置中指定它。但是在我看来注释更方便。
回答by ewernli
It seems like you can specific the usual hibnerate.cfg.xmlas a property hibernate.ejb.cfgfile.
似乎您可以将通常的指定hibnerate.cfg.xml为属性hibernate.ejb.cfgfile。
You may also define all your hibernate configuration in the usual Hibernate way: within a hibernate.xfg.xml file. You must tell the JPA implementation to use this configuration file through the hibernate.ejb.cfgfile property.
您还可以以通常的 Hibernate 方式定义所有的 Hibernate 配置:在 hibernate.xfg.xml 文件中。您必须通过 hibernate.ejb.cfgfile 属性告诉 JPA 实现使用此配置文件。
<persistence>
<persistence-unit name="manager1" transaction-type="JTA">
<jta-data-source>java:/DefaultDS</jta-data-source>
<properties>
<property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/>
</properties>
</persistence-unit>
</persistence>
Note that I never use that personally.
请注意,我从不亲自使用它。

