尝试持久化使用继承的对象的ArrayList时ArrayIndexOutOfBoundsException

时间:2011-08-07 23:40:13

标签: java google-app-engine jdo

我有3个班:人,车和模特。

Person定义了一个ArrayList属性:

List<Car> cars;

Car是一个抽象类,类Model扩展,即。

@PersistenceCapable
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public abstract class Car {

  @PrimaryKey
  @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
  protected Key key;

  public Car() {
  }

}

模型扩展Car:

@PersistenceCapable
public class Model extends Car {

  public Model() {
  }

}

我加载一个Person对象并创建一个新的Car对象,即

Person personObj = pm.getObjectById(Person.class, key);
Car newCar = new Model();

然后我尝试通过将其添加到Person Class中的“cars”属性来保持新Car,即。

personObj.getCars().add(newCar);

但是,当我尝试此App Engine时会抛出以下异常:

java.lang.ArrayIndexOutOfBoundsException: 0
    at org.datanucleus.store.mapped.scostore.FKListStore.<init>(FKListStore.java:133)
    at org.datanucleus.store.appengine.DatastoreFKListStore.<init>(DatastoreFKListStore.java:41)
    at org.datanucleus.store.appengine.DatastoreManager.newFKListStore(DatastoreManager.java:528)
    at org.datanucleus.store.mapped.MappedStoreManager.getBackingStoreForCollection(MappedStoreManager.java:729)
    at org.datanucleus.store.mapped.MappedStoreManager.getBackingStoreForField(MappedStoreManager.java:646)
    at org.datanucleus.sco.backed.List.<init>(List.java:104)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance_(Runtime.java:112)
    at com.google.appengine.tools.development.agent.runtime.Runtime.newInstance(Runtime.java:120)
    at org.datanucleus.util.ClassUtils.newInstance(ClassUtils.java:94)
    at org.datanucleus.sco.SCOUtils.newSCOInstance(SCOUtils.java:164)
    at org.datanucleus.store.mapped.mapping.AbstractContainerMapping.replaceFieldWithWrapper(AbstractContainerMapping.java:426)
    at org.datanucleus.store.mapped.mapping.CollectionMapping.postInsert(CollectionMapping.java:165)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager.runPostInsertMappingCallbacks(DatastoreRelationFieldManager.java:217)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager.access$200(DatastoreRelationFieldManager.java:48)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager$1.apply(DatastoreRelationFieldManager.java:116)
    at org.datanucleus.store.appengine.DatastoreRelationFieldManager.storeRelations(DatastoreRelationFieldManager.java:81)
    at org.datanucleus.store.appengine.DatastoreFieldManager.storeRelations(DatastoreFieldManager.java:955)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.storeRelations(DatastorePersistenceHandler.java:546)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertPostProcess(DatastorePersistenceHandler.java:304)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObjects(DatastorePersistenceHandler.java:256)
    at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:240)
    at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent(JDOStateManagerImpl.java:3185)
    at org.datanucleus.state.JDOStateManagerImpl.makePersistent(JDOStateManagerImpl.java:3161)
    at org.datanucleus.ObjectManagerImpl.persistObjectInternal(ObjectManagerImpl.java:1298)
    at org.datanucleus.ObjectManagerImpl.persistObject(ObjectManagerImpl.java:1170)
    at org.datanucleus.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:669)
    at org.datanucleus.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:694)

这听起来很类似于此处提出的类似问题:http://groups.google.com/group/google-appengine-java/browse_thread/thread/adf298a9a5b4bd03

之前有其他人见过这个问题吗?

由于

2 个答案:

答案 0 :(得分:0)

根据我的理解,personObj.getCars()会返回List <Car> cars;

由于您没有提供太多代码,我只是在做推断,但只是让List <Car> cars;cars设置为null

我不确定应用引擎平台是如何工作的,但它可能会发送一个AIOOBE而不是NPE。

无论是什么原因,你应该实例化...... cars = new ArrayList();

答案 1 :(得分:0)

我不确定这是否是您问题的根本原因(如果是,这是错误显示自身的一种相当不直观的方式),但App Engine上的JDO不支持多态。试试你的例子,不要使用抽象的“汽车”课程。