5

我需要一些帮助来弄清楚我在这里做错了什么。我试图掌握一对多的关系并遇到障碍。我尝试修改 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) 
4

3 回答 3

3

我不认为做错了什么。这似乎是供应商代码的错误。看到这个链接。没有很多信息,但他们暗示您的Long Employee.id 类型的 PK 是有问题的。

于 2009-06-29T00:33:19.783 回答
0

我确定您正在本地计算机上尝试此代码。也许这是 GAE 的 Eclipse 插件的一个错误,因为当您尝试在生产环境中运行它时,它会运行良好。我认为 App Engine 的本地版本仅返回String而不返回Longa Key,还是相反?无论如何,它都应该在生产环境中运行。

于 2011-09-25T15:26:33.693 回答
0

您的数据库的 id 列与 Long 不兼容。我认为这与父/子映射无关。如果您想确认,请分享您的创建语句。

于 2009-06-29T00:36:34.677 回答