1

我有一棵嵌套对象树,我用 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);
4

1 回答 1

1

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

于 2013-11-07T19:31:49.580 回答