Search code examples
javamongodbmorphiaodm

Morphia - How to ignore loading a property if it's of a certain type?


I have a legacy property in a MongoDB collection that's stored as a boolean, but that property has evolved and it's become an Object.

Is there a way in Morphia to ignore loading these properties when they are booleans? I'd just want to retrieve them if they are of the newer version (Object), if they are booleans I just want to ignore them.

When I try to find() a document with the property as a boolean, I get:

java.lang.ClassCastException: java.lang.Boolean cannot be cast to com.mongodb.DBObject ! at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:162) ~[morphia-1.0.1.jar:na]

Any thoughts? Thank you


Solution

  • You can use an @PreLoad annotation on a method and then remove that field from the DBObject if it's a boolean. Then the subsequent handling of the DBObject should deserialize properly. You can see an example here