Composite keys and references

306 views
Skip to first unread message

Maxim Kuleshov

unread,
Feb 6, 2012, 2:38:04 AM2/6/12
to Morphia
Hi,

Is it possible in Morphia to use both composite keys and references to
objects having such keys?

For examle, I have primary entity:

class PrimaryEntity {
@Reference
SecondaryEntity secondary;
...
}

class SecondaryEntity {
@Id
CompoundKey id;
...
}

class CompoundKey {
int fieldNumber;
String fieldString;
...
}

So, I recreate SecondaryEntitity with id set to some value of
CompoundKey type, set this entity
as a property value to PrimaryEntity and try to save PrimaryEntity's
instance.

I get the following error:

Exception in thread "main" java.lang.IllegalArgumentException: can't
serialize class com.foobar.CompoundKey
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:724)
at
com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:772)
at
com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:834)
at
com.google.code.morphia.DatastoreImpl.save(DatastoreImpl.java:828)
at com.google.code.morphia.dao.BasicDAO.save(BasicDAO.java:
109)
...

I use mongo version 2.7.2 and Morphia 1.0.0-SNAPSHOT (with 0.99 the
same result).

Scott Hernandez

unread,
Feb 6, 2012, 8:35:09 AM2/6/12
to mor...@googlegroups.com
Is your CompoundKey an inner class by any chance? Can you post a full
sample I can run?
Message has been deleted

Maxim Kuleshov

unread,
Feb 6, 2012, 11:02:59 AM2/6/12
to Morphia
Sure, I created simple maven project you can easily run (mongod
should
be started prileminary of course).

http://dl.dropbox.com/u/42545675/bar.zip

Just unpack it somewhere and run mvn exec:java

Reply all
Reply to author
Forward
0 new messages