发布于2023-05-09 20:30 阅读(1192) 评论(0) 点赞(20) 收藏(4)
我有以下持久类
@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 对象生命周期状态,并了解一旦对象在事务之外它就是 HOLLOW,因此所有关系字段都会丢失它们的值,除非你设置 datanucleus.retainValues
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/673277/e7bd16288aa85c74dfbc/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!