7

I have a JPA @Entity class which uses a @PrePersist for quite a while now. Today I wanted to add some functionality where I need the ID of that entity. This ID is generated during persist by a HIBERNATE_SEQUENCE in the database. It is usually set after em.persist(entity).

For some unknown reason the @PrePersist method is triggered... while @PostPersist simply never fires:

@Entity
public class MyEntity {

    @PrePersist
    protected void onCreate() {
        System.out.println("ExtendedEntity.onCreate()");
    }

    @PostPersist
    protected void afterCreate() {
        System.out.println("ExtendedEntity.afterCreate()");
    }
}

I'm using a JBoss v4.2 environment with Java v7+, Hibernate v3.3.1.GA and Seam v2.2.2.Final...

Are there any hidden requirements for @PostPersist to fire?

Daniel Bleisteiner
  • 3,190
  • 1
  • 33
  • 47

2 Answers2

8

For everybody else... Hibernate event listeners seem to interfere with JPA persistence events... after removing the following lines from my persistence.xml the @PostPersist callback is triggered.

<property name="hibernate.ejb.event.pre-insert"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-update"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.pre-delete"  value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-insert" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-update" value="my.hibernate.events.listeners.Listener" />
<property name="hibernate.ejb.event.post-delete" value="my.hibernate.events.listeners.Listener" />

We don't even use these anymore... they have just never been disabled completely.

Daniel Bleisteiner
  • 3,190
  • 1
  • 33
  • 47
  • For posterity, that is because Hibernate actually implements its JPA callbacks via its listeners. So rather than over-writing the listeners you'd want to make sure that you add listeners; that way both can co-exist (listeners per type are stacked). – Steve Ebersole Feb 28 '13 at 14:38
  • So my settings disabled Hibernate's own listeners it uses for JPA callbacks? Remember... @PrePersist still worked with the above entries. JPA callbacks should work no matter if I use these settings or not. – Daniel Bleisteiner Feb 28 '13 at 14:56
  • 1
    Perhaps. Not disagreeing. Just explaining what happened. We have zero enhancement requests for allowing JPA callbacks and Hibernate event listeners to work together. But be that as it may, they actually can, just not using this XML-based listener config way (which is deprecated now anyway). – Steve Ebersole Mar 05 '13 at 21:18
0

I believe that it gets called during a commit or a flush. Are you explicitly calling either of these? What I am wondering is maybe there is some delay from when the entity manager is actually persisting to the database. For example, transactions may not be committed until after the response gets written (in web apps).

Also, try using a logger, odd things can happen when calling standard out from web apps.

John Kane
  • 4,383
  • 1
  • 24
  • 42