Hi,
I'm facing some strange behaviour on the callback @PreUpdate. I am using the Java DB sample database.
I added an EntityListener on the Customer entity which has both @PrePersist and @PreUpdate methods defined, just printing stuff:
public class MyListener {
@PrePersist
private void dummyPersist(Object entity) {
if (entity instanceof Customer) {
Customer s = (Customer)entity;
System.out.println("*** in persist for: " + s.getName());
}
}
@PreUpdate
private void dummyUpdate(Object entity) {
if (entity instanceof Customer) {
Customer s = (Customer)entity;
System.out.println("*** in update for: " + s.getName());
}
}
}
When the following piece of code is executed :
...
Customer c = new Customer(668);
c.setName("The nabour of the beast");
c.setDiscountCode(new DiscountCode('N'));
c.setZip("B-1000");
em.persist(c);
c.setCity("Brussels");
...
only the @PrePersist method is called:
+[TopLink Info]: 2008.12.09 11:41:48.375--ServerSession(23491286)--TopLink, version: Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))+
+[TopLink Info]: 2008.12.09 11:41:48.640--ServerSession(23491286)--file:/C:/JCAPSprj/MyJPA/build/classes/-MyJPAPU login successful+
*** in persist for: Neighbour of the beast
Only when adding an em.flush() after the persist
...
Customer c = new Customer(668);
c.setName("The nabour of the beast");
c.setDiscountCode(new DiscountCode('N'));
c.setZip("B-1000");
em.persist(c);
em.flush();
c.setCity("Brussels");
...
, the @PreUpdate is called:
+[TopLink Info]: 2008.12.09 12:10:06.042--ServerSession(23491286)--TopLink, version: Oracle TopLink Essentials - 2.0.1 (Build b09d-fcs (12/06/2007))+
+[TopLink Info]: 2008.12.09 12:10:06.323--ServerSession(23491286)--file:/C:/JCAPSprj/MyJPA/build/classes/-MyJPAPU login successful+
*** in persist for: Neighbour of the beast
*** in update for: Neighbour of the beast
Can someone explain me why I need to force the insert to activate the callback for the update ?
Thanks in advance
kris