Java DataNucleus getObjectById为forighn键对象返回null
我有以下持久类Java DataNucleus getObjectById为forighn键对象返回null,java,mysql,servlets,jdo,datanucleus,Java,Mysql,Servlets,Jdo,Datanucleus,我有以下持久类 @PersistenceCapable public class PasswordRecovery { @PrimaryKey @Expose @Persistent(valueStrategy = IdGeneratorStrategy.UNSPECIFIED) private String id; @Persistent @Expose private long time; @Persistent @Expose private User user; public
@PersistenceCapable
public class PasswordRecovery {
@PrimaryKey @Expose
@Persistent(valueStrategy = IdGeneratorStrategy.UNSPECIFIED)
private String id;
@Persistent @Expose
private long time;
@Persistent @Expose
private User user;
public PasswordRecovery(String id, long time, User user) {
this.id = id;
this.time = time;
this.user = user;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
现在我使用下面的代码将上面的对象填充为DB值
PersistenceManager pm = pmf.getPersistenceManager();
//PasswordRecovery retObj = null;// = new PasswordRecovery();
try {
PasswordRecovery record = pm.getObjectById(PasswordRecovery.class, id);
if (null == record) {
throw new IllegalArgumentException("You are not authorized for this");
}
else {
return record;
}
} finally {
pm.close();
}
现在,当我调用,record.getUser()
时,它返回null。是否需要进行任何配置。下面是如何创建PersistenceManagerFactory的代码
Properties properties = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass","org.datanucleus.api.jdo.JDOPersistenceManagerFactory");
properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://localhost/db"); properties.setProperty("javax.jdo.option.ConnectionDriverName","com.mysql.jdbc.Driver");
properties.setProperty("javax.jdo.option.ConnectionUserName","username");
properties.setProperty("javax.jdo.option.ConnectionPassword","password");
properties.setProperty("datanucleus.autoCreateSchema","true");
properties.setProperty("datanucleus.autoCreateSchema","true");
properties.setProperty("datanucleus.autoCreateSchema","true");
properties.setProperty("datanucleus.autoCreateTables","true");
pmf = JDOHelper.getPersistenceManagerFactory(properties);
我对DataNucleus JDO很陌生?配置中是否缺少任何内容 因此假设对象是由getObjectById返回的,那么您应该查看JDO对象生命周期状态,并了解一旦对象在事务之外,它就是空的,因此所有关系字段都会丢失它们的值,除非您设置datanucleus。Retavailues这是为了实现什么@持久性(valueStrategy=IdGeneratorStrategy.UNSPECIFIED)。你有一个未指明的价值策略?!这是什么意思?为什么每个领域都有@Persistent?我对data nucleus很陌生。我想将随机生成的字符串按原样存储到主键中,因此我指定了@Persistent(valueStrategy=IdGeneratorStrategy.UNSPECIFIED)。除此之外,我的所有值都存储在DB中,因此我在每个字段上都指定了@Persistent。除非您告诉它这些“随机生成的字符串”是如何生成的,否则它不会生成任何内容(并保留您在其中输入的值)。您不需要在每个字段上都使用@Persistent,默认情况下它们会被持久化!随机生成的字符串是使用“UUID.randomuid().toString()”java代码生成的。我将从其他字段中删除@Persistent。getUser仍然返回null。如果有人遇到同样的错误,请提出建议。