Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@PostPersist not called... @PrePersist is... why?

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?

like image 211
Daniel Bleisteiner Avatar asked Feb 28 '13 14:02

Daniel Bleisteiner


People also ask

What is @PrePersist in JPA?

JPA specifies seven optional lifecycle events that are called: before persist is called for a new entity – @PrePersist. after persist is called for a new entity – @PostPersist. before an entity is removed – @PreRemove. after an entity has been deleted – @PostRemove.

What is @PrePersist in hibernate?

The @PrePersist and @PreUpdate annotations are JPA annotations introduced in JPA 1.0. Both annotations are used to configure callbacks that are triggered on specific entity lifecycle events. The @PrePersist annotation is used to configure a callback for pre-persist(pre-insert) events of the entity.

What is@ PostPersist?

The @PostPersist indicate a JPA callback method. It allows you to trigger some code through the entity life-cycle events.

What is @EntityListeners?

Annotation Type EntityListenersSpecifies the callback listener classes to be used for an entity or mapped superclass. This annotation may be applied to an entity class or mapped superclass. Since: Java Persistence 1.0.


1 Answers

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.

like image 65
Daniel Bleisteiner Avatar answered Sep 28 '22 05:09

Daniel Bleisteiner