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?
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.
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.
The @PostPersist indicate a JPA callback method. It allows you to trigger some code through the entity life-cycle events.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With