4

我有一个如下所示的集合:

db.testdata.save(
{
    "Indicator": "One",
    "secs": [
        {
            "when": "2013-03-16",
            "num": 16,
            "choices": [
                {
                    "size": "10",
                    "mult": "10"
                },
                {
                    "size": "10",
                    "mult": "10"
                }
            ]
        },
        {
            "when": "2013-03-22",
            "num": 24,
            "choices": [
                {
                    "size": "100",
                    "mult": "100"
                },
                {
                    "size": "100",
                    "mult": "100"
                }
            ]
        }
    ]
})

我正在尝试使用自定义对象检索它,如下所示:

public class Test {    
    public static void main(String args[]) throws UnknownHostException {
        Mongo mongo = new Mongo();
        DB db = mongo.getDB("at");
        DBCollection testdata = db.getCollection("testdata");

        BasicDBObject query = new BasicDBObject();
        query.put("Indicator", "One");

        CustomChainData ocd = (CustomChainData) testdata.findOne(query);

        ocd.getWhen().size();    
    }    
}

import java.util.List;    
import com.mongodb.BasicDBObject;

public class CustomChainData extends BasicDBObject{     
    public CustomChainData() {
        super();
    }    

    @SuppressWarnings("unchecked")
    public List<WhenData> getWhen() {
        return (List<WhenData>) get("secs");
    }    

    public void setWhen(List<WhenData> expirationDts) {
        put("secs", expirationDts);
    }    
}

import com.mongodb.BasicDBObject;

public class WhenData extends BasicDBObject{        
    public String getSize() {
        return (String) get("size");
    }

    public void setSize(String size) {
        put("size", size);
    }    
}

不幸的是,我不断得到:

线程“主”java.lang.ClassCastException 中的异常:com.mongodb.BasicDBObject 无法在 Test.main 中转换为 com.CustomChainData(Test.java:19)

4

2 回答 2

6

The MongoDB Java driver doesn't support automatically using sub-classes of BasicDBObject. That's why you're getting the ClassCastException; the objects returned by the driver are BasicDBObject instances, not instances of your subclasses.

One option to get this to work would be to replace the casts with constructor calls. For example, in Test, replace

CustomChainData ocd = (CustomChainData) testdata.findOne(query);

with

CustomChainData ocd = new CustomChainData(testdata.findOne(query));

and in CustomChainData, add

CustomChainData(Map m) {
    super(m);
}

This uses a copy-constructor to allow usage of your CustomChainData class with the data loaded from MongoDB. However, you'd need to apply this pattern every time you get back a BasicDBObject (for the When object as well, for example).

I prefer the approach of using a library that performs mapping between MongoDB data and Java objects. I've used Morphia in the past and been quite happy with it. Other options are listed in the MongoDB Java Language Center.

于 2013-03-12T01:28:23.783 回答
0

您需要设置对象类类型....在保存和检索对象之前使用DBCollection的setObjectClass方法....在您的情况下,它应该是-

DBCollection testdata = db.getCollection("testdata"); testdata.setObjectClass(CustomChainData.class);

于 2015-05-22T11:00:57.100 回答