0

JAva次の問題があります。モバイルオブジェクトがあります。

Calss object mobile
private ObjectId _id;
private String number;
private String nroMatricula;

取得および設定など。

MongoDBのリカバリ時。

String id = "5089e5fde4b07bf6f368366b";
DBObject soid = new BasicDBObject("_id", new ObjectId(id));
String s = MongoAccess.getSingleton().GetOneValueByKey("mobile", soid);
Mobile m = js.fromJson(s, Mobile.class);

public String GetOneValueByKey(String thecollection, DBObject key)
{
    String result = null;
    try {
        DBCollection collection = MDB.getCollection(thecollection); 
     result = JSON.serialize(collection.findOne(key));
    } catch (Exception e) {
        Logger.getLogger(MongoAccess.class.getName()).log(Level.SEVERE, e.toString());
    }
    return result;
}

データを正しく回復しますが、_id属性です。データベースにあるデータをロードしましたが、新しいIDが生成されます。これは正しい_id"5089e5fde4b07bf6f368366b"である必要がありますが、オブジェクトの料金ではありません。私は助けることができる。

4

1 に答える 1

3

解決

JsonDeserializer<ObjectId> des = new JsonDeserializer<ObjectId>() {

                    @Override
                    public ObjectId deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
                        return new ObjectId(je.getAsJsonObject().get("$oid").getAsString());
                    }
                };

                Gson gson = new GsonBuilder().registerTypeAdapter(ObjectId.class, des).create();
于 2012-10-31T17:18:42.213 に答える