Well I've found the following. For JPA-annotated classes the @PrePersist and @PreUpdate methods are only called if DN detects that the object is dirty. I have the problem that the @PrePersist and the @PreUpdate methods are the things which actually make the objects dirty - I only modify transient fields which were supposed to get merged into persistent collections before the object gets persisted. I'll fudge around this one I guess and somehow flag the object as dirty.
However, I still expect the @PostLoad method to get called if I load a collection of objects via a NamedQuery and that just doesn't seem to happen - is this a bug, as I can't find anything in the JPA spec which states that @PostLoad methods are only called on retrieving objects via ID? On May 6, 10:38 am, Simon <[email protected]> wrote: > Thanks for the example Chau, it looks identical to my configuration. > I'll try to create a mini-project and see if I can duplicate the > issue, because it definitely isn't working locally. > > On May 6, 10:33 am, Chau Huynh <[email protected]> wrote: > > > > > Here you are .. > > > import java.io.Serializable; > > import java.util.Calendar; > > import java.util.Date; > > > import javax.persistence.Entity; > > import javax.persistence.GeneratedValue; > > import javax.persistence.GenerationType; > > import javax.persistence.Id; > > import javax.persistence.PostLoad; > > import javax.persistence.PrePersist; > > import javax.persistence.PreUpdate; > > > @Entity > > public class LeaveItem implements Serializable { > > private static final long serialVersionUID = 1L; > > > @Id > > @GeneratedValue (strategy = GenerationType.IDENTITY) > > private Long id; > > > private Date dateStart; > > > transient private int start; > > > transient private int end; > > > transient private int month; > > > transient private int year; > > > @SuppressWarnings("unused") > > private int monthTag; // de-normalize > > > @SuppressWarnings("unused") > > @PostLoad > > private void postLoad() { > > Calendar calendar = Calendar.getInstance(); > > calendar.setTime(dateStart); > > start = calendar.get(Calendar.DAY_OF_MONTH); > > month = calendar.get(Calendar.MONTH) + 1; > > year = calendar.get(Calendar.YEAR); > > } > > > @SuppressWarnings("unused") > > @PrePersist > > @PreUpdate > > private void setMonthTag() { > > this.monthTag = DateUtil.toIntTag(dateStart); // to YYYYQMM > > } > > > .... > > > } > > On Thu, May 6, 2010 at 4:24 PM, Simon <[email protected]> wrote: > > > Well I'm confused then. I've annotated my class with @PostLoad, > > > @PrePersist and @PreUpdate. > > > > The @PrePersist and @PreUpdate methods never get called, whether the > > > methods are located in the @Entity annotated class or in a defined > > > @EntityListeners annotated class. I've tried leaving the persistence > > > to the DN dirty-checking persistence and also by manually re- > > > persisting the entity using the EntityManager's persist method. > > > > The @PostLoad method is only ever called if the entity is loaded via > > > the EntityManager's find method - if you retrieve a list of the > > > entities via a JPA defined query then the @PostLoad method just isn't > > > called. > > > > To the couple of people who have this working, have you got some > > > example code/configuration you can share? > > > > On May 6, 10:15 am, Chau Huynh <[email protected]> wrote: > > > > My entity classes were annotated, and work both in local and appspot. > > > Thanks > > > > > On Thu, May 6, 2010 at 4:02 PM, Simon <[email protected]> wrote: > > > > > Has anyone managed to get the JPA lifecycle callbacks to work? > > > > > > I've annotated methods with @PrePersist and @PostLoad and the methods > > > > > just never get called. I followed the documentation at > > > > > >http://www.datanucleus.org/products/accessplatform_2_0/jpa/lifecycle_. > > > .. > > > > > just to make sure that my methods had the correct signatures. > > > > > > -- > > > > > You received this message because you are subscribed to the Google > > > Groups > > > > > "Google App Engine for Java" group. > > > > > To post to this group, send email to > > > > > [email protected]. > > > > > To unsubscribe from this group, send email to > > > > > [email protected]<google-appengine-java%[email protected]> > > > <google-appengine-java%[email protected]<google-appengine-java%[email protected]> > > > > > > . > > > > > For more options, visit this group at > > > > >http://groups.google.com/group/google-appengine-java?hl=en. > > > > > -- > > > > You received this message because you are subscribed to the Google > > > > Groups > > > "Google App Engine for Java" group. > > > > To post to this group, send email to > > > [email protected]. > > > > To unsubscribe from this group, send email to > > > [email protected]<google-appengine-java%[email protected]> > > > . > > > > For more options, visit this group athttp:// > > > groups.google.com/group/google-appengine-java?hl=en. > > > > -- > > > You received this message because you are subscribed to the Google Groups > > > "Google App Engine for Java" group. > > > To post to this group, send email to > > > [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]<google-appengine-java%[email protected]> > > > . > > > For more options, visit this group at > > >http://groups.google.com/group/google-appengine-java?hl=en. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Google App Engine for Java" group. > > To post to this group, send email to [email protected]. > > To unsubscribe from this group, send email to > > [email protected]. > > For more options, visit this group > > athttp://groups.google.com/group/google-appengine-java?hl=en. > > -- > You received this message because you are subscribed to the Google Groups > "Google App Engine for Java" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group > athttp://groups.google.com/group/google-appengine-java?hl=en. -- You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
