Morphia - 使用引用列表更新对象时的序列化错误

时间:2013-11-07 16:36:55

标签: mongodb morphia

我有一个嵌套对象树,我用Morphia保存到Mongo。为了保存树,首先保存父对象,然后保存子对象,并使用对子对象的引用列表更新父对象。  我在执行更新时遇到“错误:无法序列化类ClusteringNode”,即使单个对象的保存成功也是如此。我尝试添加Serializable接口,但没有帮助。这是代码:

@Entity
public class ClusteringNode {
    @Id
    private ObjectId id;
    @Reference
    private ClusteringNode parent;

    @Reference
    private List<ClusteringNode> children;

}

saveMethod() {
  ClusteringNode node = new ClusteringNode();
    node.setName("parent");
    getDatastore().save(node);

    List<ClusteringNode> children = new ArrayList<>();
   ClusteringNode c1 = new ClusteringNode();
    c1.setName("c1");
    c1.setParent(node);
    getDatastore().save(c1);
    children.add(c1);
    ClusteringNode c2 = new ClusteringNode();
    c2.setName("c2");
    c2.setParent(node);
    getDatastore().save(c2);
    children.add(c2);
    Query<ClusteringNode> updateQuery = getDatastore().createQuery(ClusteringNode.class).field("_id").equal(node.getId());
 //  Tried both set() and addAll(), get the same result  
 //  UpdateOperations<ClusteringNode> ops = getDatastore().createUpdateOperations(ClusteringNode.class).addAll("children", children,false);
     UpdateOperations<ClusteringNode> ops = getDatastore().createUpdateOperations(ClusteringNode.class).set("children", children);
   getDatastore().update(updateQuery, ops);     
}   

这是堆栈跟踪:

java.lang.IllegalArgumentException: can't serialize class edu.harvard.iq.text.model.ClusteringNode
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:273)
at org.bson.BasicBSONEncoder.putIterable(BasicBSONEncoder.java:298)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:237)
at org.bson.BasicBSONEncoder.putMap(BasicBSONEncoder.java:313)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:235)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:174)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:120)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:289)
at com.mongodb.OutMessage.writeUpdate(OutMessage.java:175)
at com.mongodb.OutMessage.update(OutMessage.java:62)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:325)
at com.mongodb.DBCollection.update(DBCollection.java:178)
at org.mongodb.morphia.DatastoreImpl.update(DatastoreImpl.java:1142)
at org.mongodb.morphia.DatastoreImpl.update(DatastoreImpl.java:1100)
at org.mongodb.morphia.DatastoreImpl.update(DatastoreImpl.java:1106)
at org.mongodb.morphia.DatastoreImpl.update(DatastoreImpl.java:999)
at edu.harvard.iq.text.model.NodeHierarchyTest.verySimpleTest(NodeHierarchyTest.java:116)

如果不是addAll(),我会单独添加每个对象,它可以工作:

 UpdateOperations<ClusteringNode> ops = getDatastore().createUpdateOperations(ClusteringNode.class).add("children", children.get(0));
    getDatastore().update(updateQuery, ops);
    ops = getDatastore().createUpdateOperations(ClusteringNode.class).add("children", children.get(1));
    getDatastore().update(updateQuery, ops);

1 个答案:

答案 0 :(得分:1)

行。这看起来像我刚刚修复的错误。你能用0.106-SNAPSHOT再试一次,看看它是否仍然出错?