Rahul Borkar
Rahul Borkar

Reputation: 2762

DataNucleus getObjectById returns null for forighn key objetcs

I have following persistable class

@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;
}

Now I am using following code to get the above object filled with DB values,

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();
    }

Now when I call,record.getUser(), it returns null. Is there any configuration which needs to be done. Following is code for how I create the 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);

I am pretty new to DataNucleus JDO? Is there anything missing in configuration?

Upvotes: 1

Views: 164

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

So presuming the object is returned by getObjectById then you should look at JDO object lifecycle states, and understand that once an object is outside the transaction it is HOLLOW, hence all relation fields lose their values unless you set datanucleus.retainValues

Upvotes: 1

Related Questions