I have a tree of nested objects that I'm saving to Mongo with Morphia. In order to save the tree, first I save the parent object, then I save the children, and update the parent with a list of references to the children. I get "ERROR: can't serialize class ClusteringNode" when executing the update, even though the save of the individual objects is successful. I tried adding Serializable interface and that didn't help. Here is the code:
@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);
}
Here is the stack trace:
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)
If instead of addAll(), I add each object individually, it works:
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);
OK. That looks like a bug i just fixed. Can you try again with 0.106-SNAPSHOT and see if it still errors out that way?