Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

@PreUpdate not called

843830Dec 9 2008 — edited Dec 17 2008
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
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 14 2009
Added on Dec 9 2008
3 comments
969 views