Declaring Hibernate event listeners in a JPA environment

events, hibernate, java, jpa, listener

Solution

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.

Problem

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. I've read that I must declare in the Hibernate configuration file `*cfg.xml` something like this: ``` <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? Thanks in advance.

Original source