@PrePersist not called when merge() was invoked on instance
567350Apr 23 2007 — edited Apr 24 2007I am persisting new object instances using TopLink JPA and need to perform initialization tasks in the @PrePersist lifecycle listener method.
As the object graph contains detached objects in relational-attributes of the new object instance, merge() [with cascade=all] is invoked on the new object instance prior to the persist() call, e.g.:
final EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
/* merge related object instances */
obj_to_be_persisted=em.merge(obj_to_be_persisted);
//persist (save) new object instance:
em.persist(obj_to_be_persisted);
em.getTransaction().commit();
em.close();
In this case the @PrePersist-annotated method is NOT invoked by TopLink JPA. Tests without invoking merge() DID invoke the @PrePersist method.
I would guess this to be a bug: I wrote a similar testcase for Apache OpenJPA and in this case the above scenario (merge, persist -> invoke @PrePersist) works fine.
We are using TopLink JPA 2.0-b41-beta2 (03/30/2007) in Java SE environment.
regards,
Hans