Announcing Hibernate Event Listeners in JPA

Guy van

I am working on a project developed in the Java EE 5 environment. I want to know how I can declare a Hibernate event listener so that I can be informed about the CRUD operation.

I read that I should declare something like this in the Hibernate *cfg.xml configuration file:

 <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 that I do not have such a file in the project. We use JPA (with Hibernate as the base implementation). Do you know if I need to create this file? If so, where should I put it?

Thanks in advance.

+7
java events listener hibernate jpa
source share
4 answers

In your 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 docs, look at "Table 2.1. Hibernate Entity Manager Specific Properties" for all applicable properties.

+6
source share

I assume you are using annotations? If so, you can use the @EntityListeners annotation to do this, for example:

 @MappedSuperclass @EntityListeners(AbstractEntityListener.class) public abstract class AbstractEntity { ... } 

The listener class of your entity may 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 various annotations that allow you to capture different events, for example @PrePersist , @PreUpdate , @PostLoad , etc.

+5
source share

Note that you can also specify this with annotations of callback methods. Either embed them in the object 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 think you can also specify this in the XML configuration. But, in my opinion, it is more convenient to use the annotation.

+4
source share

It looks like you can specify the usual hibnerate.cfg.xml as the hibernate.ejb.cfgfile property.

You can also define your entire hibernation configuration in normal hibernation mode path: in the hibernate.xfg.xml file. You must say about the JPA implementation to use this configuration file through the hibernate.ejb.cfgfile Property.

See this post or this one .

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

Please note that I never use this personally.

+2
source share

All Articles