本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

DataNucleus getObjectById 为外键对象返回 null

发布于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黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)