简体   繁体   English

如何通过Generic Class从MongoDB中检索POJO

[英]How to retrieve a POJO from MongoDB via a Generic Class

I'm new to Mongo, and am trying to use it to implement a cache internally in our application. 我是Mongo的新手,我正在尝试使用它在我们的应用程序内部实现缓存。 We have a defined cache interface (public Cache(K, V) ) with several alternative implementations (HashTable, JCS etc). 我们有一个定义的缓存接口(公共缓存(K,V))和几个替代实现(HashTable,JCS等)。 I need to create a MongoDB implementation for some hard (ie expensive) to calculate data. 我需要为一些硬(即昂贵的)计算数据创建一个MongoDB实现。 The cache data will either be a POJO or a List of POJO's. 缓存数据将是POJO或POJO列表。

My problem is getting the Mongo response back into a POJO or (the bit that's eluded me so far), into a List of POJOs. 我的问题是将Mongo响应变回POJO或(到目前为止我没有得到的位),进入POJO列表。

Code so far: 代码到目前为止:

public class MongoDBCache<K, V> implements Cache<K, V>
{

private String name = null;

public MongoDBCache(String name)
{
    this.name = name;
}

public V get(K key)
{
    V result = null;
    try
    {

            DB mdb = getMongoDB();

            DBCollection mcol = mdb.getCollection(name);

            BasicDBObject query = new BasicDBObject("_id", key.toString());
            DBCursor cursor = mcol.find(query);

            if (cursor.hasNext())
            {
                Gson gson = new Gson();
                DBObject dbobj = cursor.next();

                Class type = ????;

                result = (V) gson.fromJson(dbobj.get("obj").toString(), type);

            }


    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return result;
}
}

I can kind of make this work if the value is just a POJO (can get the type on the put method, not ideal but works), but I can't figure out how to make it work for a List. 如果值只是一个POJO(可以在put方法上得到类型,不理想但有效),我可以做这个工作,但我无法弄清楚如何让它适用于List。 As an example, a ArrayList <Long> ends up as ArrayList <Double> . 例如,ArrayList <Long>最终为ArrayList <Double>

Any suggestions? 有什么建议么? Something I can do in GSON? 我能在GSON做些什么吗? Something I can do with reflection? 我可以用反射做些什么吗? (I'm not tied into GSON, or any other library, this is just my current attempt). (我没有绑定到GSON或任何其他库,这只是我目前的尝试)。

Thanks, Alan 谢谢,艾伦

If you could save also your class type into the database along with the object, you could infer the right class using something like: 如果您可以将类类型与对象一起保存到数据库中,则可以使用以下内容推断出正确的类:

 if (cursor.hasNext())
        {
            Gson gson = new Gson();
            DBObject dbobj = cursor.next();

            Class type = Class.forName(dbobj.get("class").toString());

            result = (V) gson.fromJson(dbobj.get("obj").toString(), type);

        }

you can get the proper string with code like this: 您可以使用以下代码获取正确的字符串:

SomeClass object = ...
Class c = object.getClass();
String cn = c.toString();

Edit 编辑

Pay attention that when you want to store on database the class type and you are dealing with generics, due to type erasure, you cannot do something like this: 注意当你想在数据库中存储类类型并且你正在处理泛型时,由于类型擦除,你不能做这样的事情:

ArrayList<Long> l = new ArrayList<Long>();  
String clazz = l.getClass

since clazz will store: 因为clazz会存储:

java.util.ArrayList

instead you need to do something like this 相反,你需要做这样的事情

Type listType = new TypeToken<ArrayList<Long>>() {}.getType();
String clazz = l.getClass();

that will return you: 那会回报你:

java.util.ArrayList<java.lang.Long>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM