Hi,
I created the following class :
package com.maskhot.fam.model;
import java.io.Serializable;
import java.util.Date;
import javax.jdo.annotations.Column;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
@PersistenceCapable(identityType = IdentityType.APPLICATION,
detachable="true")
public abstract class ModelBase implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String PROP_KEY = "key";
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
// @Extension(vendorName="datanucleus", key="gae.encoded-pk",
value="true")
// @Extension(vendorName="datanucleus", key="gae.pk-id", value="true")
@Column(name=PROP_KEY)
private Long key;
public static final String PROP_CREATED = "created";
@Persistent
@Column(name=PROP_CREATED)
private Date created;
public static final String PROP_UPDATED = "updated";
@Persistent
@Column(name=PROP_UPDATED)
private Date updated;
public void beforeSave() {
if (created == null) {
created = new Date();
}
updated = new Date();
System.out.println("ModelBase::beforeSave");
System.out.println("created "+created);
System.out.println("updated "+updated);
}
public void afterLoad() {}
public Date getUpdated() {
return updated;
}
public void setKey(Long key) {
this.key = key;
}
public Long getKey() {
return key;
}
/**
* @return the created
*/
public Date getCreated() {
return created;
}
/**
* @param created the created to set
*/
public void setCreated(Date created) {
this.created = created;
}
/**
* @param updated the updated to set
*/
public void setUpdated(Date updated) {
this.updated = updated;
}
}
All my other classes to be persisted will extend this ModelBase class.
In my Dao, I added a hook (as I read in GAE cookbook) :
PersistenceManager pm = PMF.get().getPersistenceManager();
pm.addInstanceLifecycleListener(new PersistHookListener(),
PersistHooks.class);
with the following classes :
package com.maskhot.fam.model;
public interface PersistHooks {
void beforeSave();
void afterLoad();
}
package com.maskhot.fam.server.common;
import javax.jdo.listener.InstanceLifecycleEvent;
import javax.jdo.listener.LoadLifecycleListener;
import javax.jdo.listener.StoreLifecycleListener;
import com.maskhot.fam.model.PersistHooks;
public final class PersistHookListener implements
StoreLifecycleListener,
LoadLifecycleListener {
public void postStore(InstanceLifecycleEvent ev) {
System.out.println("PersistHookListener.postStore");
}
public void preStore(InstanceLifecycleEvent ev) {
System.out.println("PersistHookListener.preStore");
((PersistHooks)ev.getPersistentInstance()).beforeSave();
}
public void postLoad(InstanceLifecycleEvent ev) {
System.out.println("PersistHookListener.postLoad");
((PersistHooks)ev.getPersistentInstance()).afterLoad();
}
}
Unfortunately, the hook does not work with inheritance. If I do not
use inheritance, I succeed in filling my created and/or updated
fields.
Does anyone have the same issue, or do I do something wrong?
--
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.