在JDO / Datanucleus中持续收集接口的问题。 “无法分配类型的对象..”

时间:2010-09-28 00:31:13

标签: jdo datanucleus

我在尝试持久化具有接口集合的对象时遇到错误,我想要保存几种不同类型的对象。似乎几乎是随机发生的。有时在重新启动后它可以正常工作(尽管我可能做错了。)

class CommentList {

@Persistent
@Join
ArrayList<IComment> = new ArrayList<IComment>();

}

其他地方......

CommentList cl = new CommentList();

cl.addComment( new SimpleComment() );
cl.addComment( new SpecialComment() );

repo.persist( cl );

我可以看到我的数据库中已经创建了连接表以及每个IComment实现类的ID字段。

SimpleComment和SpecialComment实现IComment。如果我只是添加一个SimpleComment它工作正常。一旦我开始尝试添加其他类型的对象,我就开始得到错误。

错误即将到来

java.lang.ClassCastException: Field "com.myapp.model.CommentList.comments" is a reference field (interface/Object) of type com.myapp.behaviours.IComment but DataNucleus is unable to assign an object of type "com.myapp.model.ShortComment" to this field. You can only assign this field to a type specified by the "implementation-classes" extension attribute.
at org.datanucleus.store.mapped.mapping.MultiMapping.setObject(MultiMapping.java:220)
at org.datanucleus.store.mapped.mapping.ReferenceMapping.setObject(ReferenceMapping.java:526)
at org.datanucleus.store.mapped.mapping.MultiMapping.setObject(MultiMapping.java:200)
at org.datanucleus.store.rdbms.scostore.BackingStoreHelper.populateElementInStatement(BackingStoreHelpe
r.java:135)
at org.datanucleus.store.rdbms.scostore.RDBMSJoinListStoreSpecialization.internalAdd(RDBMSJoinListStore
Specialization.java:443)
at org.datanucleus.store.mapped.scostore.JoinListStore.internalAdd(JoinListStore.java:233)

当它保存时,如果我重新启动服务器并尝试查询注释列表,我会返回空值。

我正在使用mysql后端 - 如果我切换到db4o它可以正常工作。

如果有任何信息有用,请告诉我。

如果您知道我可能出错的地方,或者可以提供一些示例代码来持久收集实现相同界面的不同对象。

感谢您的帮助。

汤姆

2 个答案:

答案 0 :(得分:0)

当我使用接口时,我只启用了dynamicSchemaUpdates(一些具有类似名称的持久性属性),并在需要时添加了FK。日志提供了我认为的所有SQL

答案 1 :(得分:0)

我通过指定

来解决这个问题
<extension implemention-classes="SimpleComment SpecialComment"/>

表示我的pacakge.jdo中的字段cl

相关问题