添加新字段时Datanucleus JDODetachedFieldAccessException

时间:2014-08-27 17:10:49

标签: jdo datanucleus

我有以下课程:

public class MyClass
{
    private int oldField;
} 

具有以下元数据文件:

<?xml version="1.0" encoding="utf-8"?>
<jdo xmlns="http://java.sun.com/xml/ns/jdo/jdo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdo http://java.sun.com/xml/ns/jdo/jdo_2_0.xsd">
  <package name="my.package">
    <class name="MyClass" detachable="true">
      <inheritance strategy="new-table"/>
      <field name="oldField" null-value="exception"/>
    </class>
  </package>
</jdo>

当我添加新字段private int newField时,添加相应的元数据条目:

<?xml version="1.0" encoding="utf-8"?>
<jdo xmlns="http://java.sun.com/xml/ns/jdo/jdo" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/jdo/jdo http://java.sun.com/xml/ns/jdo/jdo_2_0.xsd">
  <package name="my.package">
    <class name="MyClass" detachable="true">
      <inheritance strategy="new-table"/>
      <field name="oldField" null-value="exception"/>
      <field name="newField"/>
    </class>
  </package>
</jdo>

我在尝试获取oldField字段的值时遇到此异常:

javax.jdo.JDODetachedFieldAccessException: You have just attempted to access field "oldField" yet this field was not detached when you detached the object. Either dont access this field, or detach it when detaching the object.
如果删除newField,则不会抛出

。在元数据文件中添加default-fetch-group="true"newField标记无法解决问题。

持久性代码如下:

public MyClass addMyClass(MyClass myClass)
{
    getPM().makePersistent(myClass);
    return getPM().detachCopy(myClass);
}

0 个答案:

没有答案