tags:

views:

22

answers:

1

I have a model classes like

public class MyClass extends ReflectionDBObject {
    private List<NiceAttribute> attributes;    
    ...
}

public class NiceAttribute extends ReflectionDBObject {
    ...
}

I create it in a typesafe way, like

List<NiceAttribute> attrs = new ArrayList<NiceAttribute>();
attrs.add(new NiceAttribute());
MyClass myClass = new MyClass();
myClass.setAttributes(attrs);

then save it to mongo, and retrieve with a code like

DBCollection col = ...;
col.setObjectClass(MyClass.class)
MyClass foundObject = (MyClass)col.findOne();

But the problem is that foundObject's attributes becomes a list of BasicDBObject. Looks like a driver can not (or does not want to) detect a list items type. Is this a driver limitation, or I missed something? What would be an elegant workaround for the problem?

BTW, I know about Morphia etc. Maybe it solves the problem. But my project is tiny, and I don't want to complicate things having one more layer of abstraction.

A: 

You should use Morphia. It adds support for POJOs and embedded objects (and collections). It doesn't have any of the limitations the driver does about requiring your classes to look like a Map<String, Object>.

Scott Hernandez