エンティティの埋め込みコレクションをmongoに保存および取得するという単純な問題があります。私は論文の質問をチェックしました:
クラスをシリアライズするには?そしてMongodbはオブジェクトのリストを保存します
私が理解しているのは、オブジェクトのクラスがRefactionDBObjectを拡張する必要があるリストオブジェクトを保存することです。これはオブジェクトを保存するために機能しましたが、埋め込まれたコレクションでオブジェクトを取得しても機能しません。
ここで、埋め込まれたエンティティの取得が機能しないことを示す簡単なテストを示します。
@Test
public void whatWasStoredAsEmbeddedCollectionIsRetrieved2() {
BasicDBObject country = new BasicDBObject();
country.put("name", "Bulgaria");
List<City> cities = Lists.newArrayList(new City("Tarnovo"));
country.put("listOfCities", cities);
DBCollection collection = db().get().getCollection("test_Collection");
collection.save(country);
DBCursor object = collection.find(new BasicDBObject().append("name", "Bulgaria"));
DBObject returnedCity = object.next();
DBObject embeddedCities = (DBObject) returnedCity.get("listOfCities");
System.out.println(embeddedCities);
}
シティクラスはこちら
class City extends ReflectionDBObject {
String name;
City() {
}
City(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof City)) return false;
City city = (City) o;
if (name != null ? !name.equals(city.name) : city.name != null) return false;
return true;
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
@Override
public String toString() {
return "City{" +
"name='" + name + '\'' +
'}';
}
}
System.out.println ステートメントの出力is [ { "_id" : null }]
埋め込みオブジェクトとその中に埋め込まれたリストを取得するにはどうすればよいでしょうか。