我有两个servlet:
我存储用户的地方:
pm = PMF.get().getPersistenceManager();
Key key = KeyFactory.createKey(PersistentUser.class.getSimpleName(), "alfred");
PersistentUser user = new PersistentUser();
user.setKey(key);
user.setLogin("alfred");
try {
pm.makePersistent(user);
} finally {
pm.flush();
pm.close();
}
另一个我想读它的地方:
pm = PMF.get().getPersistenceManager();
Key k = KeyFactory.createKey(PersistentUser.class.getSimpleName(), "alfred");
PersistentUser e;
try {
e = pm.getObjectById(PersistentUser.class, k);
log.info(e.getLogin());
response.getOutputStream().println(e.getLogin());
} finally {
pm.flush();
pm.close();
}
所以,在使用户持久化时我没有遇到任何问题,但是我无法检索另一个servlet中的对象来获取此错误:
javax.jdo.JDOObjectNotFoundException:无法使用密钥PersistentUser(“login”)检索类型PersistentUser的实体
我正在使用标准的PMF课程。我的PersistentUser类如下:
@PersistenceCapable
public class PersistentUser implements Serializable{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String login;
public PersistentUser() {}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public Key getKey() {
return key;
}
public void setKey(Key key) {
this.key = key;
}
}
我的jdoconfig文件是:
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">
<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass"value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
<property name="datanucleus.appengine.singletonPMFForName" value="true"/>
</persistence-manager-factory>
</jdoconfig>