DataNucleus getObjectById为外键对象返回null

时间:2015-01-20 07:31:47

标签: 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 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的新手?配置中是否有任何缺失?

1 个答案:

答案 0 :(得分:0)

假设对象是由getObjectById返回的,那么你应该查看JDO对象生命周期状态,并理解一旦对象在事务之外它是HOLLOW,因此除非你设置datanucleus.retainValues,否则所有关系字段都会丢失它们的值