views:

1239

answers:

4

Hy guys,

I am working on a project developed in JEE 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.

+1  A: 

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.

ewernli
Thanks,But I think these are the JPA entity listeners. I tried with these ones and I am encountering some issues. I want to try directly with Hibernate event listeners.
OK, I deleted my answer then.
ewernli
A: 

I assume you are using annotations? If so, you can use the @EntityListeners annotation to do that, like so:

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

Henning
No, the question is actually about where to put hibernate specific config when using JPA.
ewernli
@ewernli: "I want to know how I can declare a Hibernate event listener so that I can be informed when CRUD operation are performed." My answer solves the problem without any cumbersome Hibernate stunts, I think.
Henning
I've undeleted my old answer so that you can see the answer from the OP. For some reason he doesn't want to use entity listener. I share you opinion otherwise.
ewernli
@ewernli: Ah, I see! Makes sense now. Still, IMHO the OP should rather resolve his issues with the JPA entity listeners instead of doing anything crazy. +1 for your answer!
Henning
mtpettyp
+1  A: 

It seems like you can specific the usual hibnerate.cfg.xml as a property 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.

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>

Note that I never use that personally.

ewernli
+2  A: 

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.

mtpettyp