Dealing with variably-typed fields

21 views
Skip to first unread message

Aaron Fabbri

unread,
Dec 18, 2014, 12:58:21 PM12/18/14
to mor...@googlegroups.com
I'm trying to figure out how to persist/reload a class that has fields with varying types.. e.g.

class MyThing {
   String stuff;
   Object context;
}

Where the runtime type of .context varies (in my software, it is place plugins can store their state as a POJO and I persist it without caring about its type.  This allows new plugins to be written without changing core code.)

These seem to serialize fine, but when I try to load them, I'll get a failure like this

java.lang.RuntimeException: java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:171)
at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:607)
at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:582)
<snip>
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.mongodb.DBObject
at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:160)

I'm a Morphia n00b so I'm not sure where to start.   Thanks!
Reply all
Reply to author
Forward
0 new messages