尝试将子项添加到拥有的一对多的父项时ClassCastException。 (在google appengine上使用jdo)

时间:2009-06-28 22:57:41

标签: java google-app-engine jdo google-cloud-datastore

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

3 个答案:

答案 0 :(得分:3)

我不认为做错了什么。这似乎是供应商代码的一个错误。见this link。有很多信息,但他们认为您的类型Long Employee.id上的PK存在问题。

答案 1 :(得分:0)

您的数据库的id列与Long不兼容。我不认为这与父/子映射有任何关系。如果您想确认,请分享您的创建声明。

答案 2 :(得分:0)

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