How to get a correctly printed list from ReflectionDBObject

I have model classes like

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

public class NiceAttribute extends ReflectionDBObject {
    ...
}

I create it as a type e.g.

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

then save it in mongo and get with a code like

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

But the problem is what foundObject attributesbecomes a list BasicDBObject. It seems that the driver cannot (or does not want) detect the type of list items. Is this a limitation for the driver, or am I missing something? What would be an elegant way to solve the problem?

By the way, I know about Morphia, etc. Maybe this solves the problem. But my project is tiny, and I don’t want to complicate things that have another layer of abstraction.

+5
source share
2 answers

, , . . , :

public class Release extends ReflectionDBObject {

    //other fields omitted    

    private List<ReleaseDetailsByTerritory> releaseDetailsByTerritory = new ArrayList<ReleaseDetailsByTerritory>();

}

public class ReleaseDetailsByTerritory extends ReflectionDBObject { //...}

, :

    releaseColl.setObjectClass(Release.class);
    releaseColl.setInternalClass("ReleaseDetailsByTerritory.0", ReleaseDetailsByTerritory.class);
    Release r = (Release) releaseColl.findOne();
    //the internal list will contain ReleaseDetailsByTerritory type objects (not DBObjects)
    System.out.println(r.getReleaseDetailsByTerritory().get(0).getClass().getName());

, (, , , ) . - :

releaseColl.setInternalClass("ReleaseDetailsByTerritory", ReleaseDetailsByTerritory.class);

releaseColl.setInternalClass("ReleaseDetailsByTerritory.*", ReleaseDetailsByTerritory.class);

:

releaseColl.setInternalClass("ReleaseDetailsByTerritory.0", ReleaseDetailsByTerritory.class);
releaseColl.setInternalClass("ReleaseDetailsByTerritory.1", ReleaseDetailsByTerritory.class);
releaseColl.setInternalClass("ReleaseDetailsByTerritory.2", ReleaseDetailsByTerritory.class);
+4

. POJO ( ). - , , Map<String, Object>.

-1

All Articles