1

为了找到另一个问题,我的测试提出了以​​下代码。

public class TestPersistance {
 private static final PersistenceManagerFactory PMF = JDOHelper.getPersistenceManagerFactory("datanucleus.properties");
 public static final PersistenceManager pm = PMF.getPersistenceManager();
 static final TestUserDataDB ud = new TestUserDataDB();

 public static void main(String args[])
 {
  TestPersistance tp = new TestPersistance();
  tp.createData();
 }

  @Test  public void createData()
 {
  assertTrue("Null machined id at start", ud.machineId != null);
  pm.currentTransaction().begin();
   try
   {
  pm.makePersistent(ud);
   }
   finally
   {
  pm.currentTransaction().commit();
   }
   assertTrue("Null machined id at end", ud.machineId != null);
 }
}

第二个断言失败的地方。IE。我要求持久化的对象正在被 makePersistent 调用更改。数据正在存储在数据库中。有任何想法吗?任何人都可以证实这一点。使用 jdo-api-3.0.jar datanucleus-core-2.2.0-release.jar datanucleus-enhancer-2.1.3.jar datanucleus-rdbms-2.2.0-release.jar mysql-connector-java-5.1.13.jar

在 Eclipse 中使用 MySql 数据库。

@PersistenceCapable
public class TestUserDataDB {

 @PrimaryKey
 @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
 public Long id;

 @Persistent
 public String userid = "test1";
 @Persistent
 public String machineId = "test2";

 // local userid
 @Persistent
 public long uid = 1L;

 @Persistent
 public long systemTime = 123L;
 public long chk = 1234L;
 public long createTime = System.currentTimeMillis();

 public TestUserDataDB()
 {
 }

 @Override
 public String toString() {
  return "TestUserDataDB [chk=" + chk + ", createTime=" + createTime
    + ", id=" + id + ", machineId=" + machineId + ", systemTime="
    + systemTime + ", uid=" + uid + ", userid=" + userid + "]";
 }



}

属性文件是

javax.jdo.PersistenceManagerFactoryClass=org.datanucleus.jdo.JDOPersistenceManagerFactory
datanucleus.metadata.validate=false

javax.jdo.option.ConnectionDriverName=com.mysql.jdbc.Driver
javax.jdo.option.ConnectionURL=jdbc:mysql://localhost/test
javax.jdo.option.ConnectionUserName=root
javax.jdo.option.ConnectionPassword=yeahRight
datanucleus.autoCreateSchema=true
datanucleus.validateTables=false
datanucleus.validateConstraints=false
4

2 回答 2

2

Why are you accessing fields directly ? Is the accessing class declared as PersistenceAware ? Well it isn't so you can't do that - use the getters. What is "ud" object state before persist ? (transient?) what is it after persist ? (hollow?) What does the log say ? Chances are that it is in hollow state and then you access a field directly and it has no value (by definition, as per the spec) ... but since you didn't bother calling the getter it hasn't a chance to retrieve the value. And you likely also don't have "RetainValues" persistent property set

Suggest you familiarise yourself with the JDO spec and object lifecycle states

于 2011-01-08T07:33:09.033 回答
0

In some cases, it is necessary to access deserialized objects' attributes directly (i.e. if using GSON library for JSON serialization). In that case you can use:

MyClass copy = myPersistencyManager.detachCopy(myRetrievedInstance);
于 2013-09-13T10:12:23.240 回答