我需要一些帮助来弄清楚我在这里做错了什么。我试图掌握一对多的关系并遇到障碍。我尝试修改 Employee 和 ContactInfo 示例以进行一对多映射:
如果我同时创建父级(员工)和子级(联系人),然后调用 makePersistent,一切正常。但是,如果我尝试将子对象添加到已经持久的父对象,则会收到 java.lang.ClassCast 异常。完整的堆栈跟踪位于帖子的底部。
这是中断的代码(如果我将 makePersistent() 调用移到 add() 之后,一切正常:
public void testOneToMany(){
pm = newPM();
Employee e = new Employee("peter");
pm.makePersistent(e);
Contact c = new Contact("123 main");
List<Contact> contacts = e.getContacts();
contacts.add(c); // here I get java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
}
这是父类
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Employee {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long id;
@Persistent
private String name;
@Persistent(mappedBy="employee")
private List<Contact> contacts;
public Employee(String e){
contacts = new ArrayList<Contact>();
name = e;
}
List<Contact> getContacts(){
return contacts;
}
Long getId(){
return id;
}
public String getName(){
return name;
}
}
这是子类
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Contact {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String streetAddress;
@Persistent
private Employee employee;
public Contact(String s){
streetAddress = s;
}
public String getAddress(){
return streetAddress;
}
public Employee getEmployee(){
return employee;
}
}
这是完整的堆栈跟踪:
java.lang.ClassCastException: java.lang.Long cannot be cast to
java.lang.String
at org.datanucleus.store.appengine.DatastoreRelationFieldManager
$1.setObjectViaMapping(DatastoreRelationFieldManager.java:148)
at org.datanucleus.store.appengine.DatastoreRelationFieldManager
$1.apply(DatastoreRelationFieldManager.java:108)
at
org.datanucleus.store.appengine.DatastoreRelationFieldManager.storeRelations
(DatastoreRelationFieldManager.java:80)
at
org.datanucleus.store.appengine.DatastoreFieldManager.storeRelations
(DatastoreFieldManager.java:770)
at
org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject
(DatastorePersistenceHandler.java:231)
at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent
(JDOStateManagerImpl.java:3067)
at org.datanucleus.state.JDOStateManagerImpl.makePersistent
(JDOStateManagerImpl.java:3043)
at org.datanucleus.ObjectManagerImpl.persistObjectInternal
(ObjectManagerImpl.java:1258)
at org.datanucleus.sco.SCOUtils.validateObjectForWriting
(SCOUtils.java:1365)
at
org.datanucleus.store.mapped.scostore.ElementContainerStore.validateElementForWriting
(ElementContainerStore.java:401)
at
org.datanucleus.store.mapped.scostore.FKListStore.validateElementForWriting
(FKListStore.java:764)
at org.datanucleus.store.mapped.scostore.FKListStore.internalAdd
(FKListStore.java:503)
at org.datanucleus.store.mapped.scostore.AbstractListStore.add
(AbstractListStore.java:123)
at org.datanucleus.sco.backed.List.add(List.java:752)
at com.btg.plyus.scratch.JDOTest.testOneToMany(JDOTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at com.btg.plyus.test.BaseTest.runTest(BaseTest.java:79)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run
(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)