Can't serialize class error when using references

393 views
Skip to first unread message

Johan Haleby

unread,
Jun 7, 2012, 9:48:14 AM6/7/12
to Morphia
Hi, 

I've just started to use References in Morphia but immediately I run into an error. I have an entity that references another entity:

public class Entity1 {
    @Reference(value = "entity2", lazy = true)
     private Entity2 entity2;

     ...
}

Entity2 looks like this:

public class Entity2 {
    @Id
     private Entity2Id Id;

     ...
}

Entity2Id looks like this:

public class Entity2Id {
    private String x;
    private String y;     

The error message I get when persisting an instance of Entity1 is:

java.lang.IllegalArgumentException: can't serialize class com.mycompany.Entity2Id
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:234)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
at org.bson.BasicBSONEncoder._putObjectField(BasicBSONEncoder.java:224)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:140)
at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
at com.mongodb.OutMessage.putObject(OutMessage.java:142)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:346)
at com.mongodb.DBCollection.update(DBCollection.java:165)
at com.mongodb.DBCollection.save(DBCollection.java:736)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:767)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:830)
at com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:824)
at com.google.code.morphia.dao.BasicDAO.save(BasicDAO.java:109)

I'm using the snapshot version of Morphia. Can't you use Morphia with references that has compund ids like this or am I doing something wrong?

Regards,
/Johan

Johan Haleby

unread,
Jun 7, 2012, 10:12:19 AM6/7/12
to Morphia
I think this is a bug i Morphia. In the Mapper class there's a method called keyToRef that returns:

new DBRef(null, key.getKind(), key.getId());

When the Id of the key is not a type that Mongo recognizes it will fail with the exception I described in the previous message. If I change it to:

return new DBRef(null, key.getKind(), toDBObject(key.getId()));

for non-Mongo objects it works.

Regards,
/Johan

Johan Haleby

unread,
Jun 7, 2012, 10:36:35 AM6/7/12
to Morphia
There seems to be a related problem because if you apply my code change then a ClassCastException is thrown when the ReferenceMapper#readSingle(..) method is called because of this line:

DBRef dbRef = (DBRef) mf.getDbObjectValue(dbObject);

The object I get back is not a DBRef but a BasicDBObject.

/Johan

Maaz

unread,
Jun 8, 2012, 9:21:35 AM6/8/12
to mor...@googlegroups.com
what you should be doing is 
public class Entity2Id implements Serializable

That's what i am seeing from exception

Thanks

Johan Haleby

unread,
Jun 8, 2012, 10:09:19 AM6/8/12
to mor...@googlegroups.com
Thanks for your tip, perhaps it could be that simple. Right now I've reverted my code since my work-around was too complex. I'll see if your suggestion works next time I try out references.

Regards,
/Johan
Reply all
Reply to author
Forward
0 new messages