monganez is MongoDB simple data mapper for Java. This library helps to convert Java Object(Map/Iterable/POJO) to the DBObject(from MongoDB Java Driver). For example, below.
public class SaveTest {
private DBCollection collection;
@Before
public void setUp() throws UnknownHostException, MongoException{
Mongo mongo = new Mongo("localhost");
DB db = mongo.getDB("test");
collection = db.getCollection("saved");
}
@Test
public void testNestedPojoListAsAttributeToDBObject() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
String id = Long.toString(System.currentTimeMillis());
Foo foo = new Foo();
foo.setId(id);
foo.setCount(40);
Bar bar1 = new Bar();
bar1.setId("bar1_" + id);
bar1.setStringList(new ArrayList(Arrays.asList("hoge", "hogehoge")));
Bar bar2 = new Bar();
bar2.setId("bar2_" + id);
bar2.setStringList(new ArrayList(Arrays.asList("foo", "bar", "hoge")));
foo.setBarList(new ArrayList(Arrays.asList(bar1, bar2)));
Map map = new HashMap();
map.put("foo", foo);
DBObjectEncoder encoder = new DBObjectEncoder();
collection.save(encoder.encode(map));
}
}
In MongoDB, below.
> db.saved.find();
{ "_id" : ObjectId("4dda9b15f769a51d9f924c8d"), "foo" : { "id" : "1306172181493", "count" : 40, "class" : "net.wrap_trap.monganez.Foo", "bar" : null, "barList": { "collectionValue" : [
{
"id" : "bar1_1306172181493",
"stringList" : {
"collectionValue" : [
"hoge",
"hogehoge"
],
"collectionClass" : "java.util.ArrayList"
},
"class" : "net.wrap_trap.monganez.Bar"
},
{
"id" : "bar2_1306172181493",
"stringList" : {
"collectionValue" : [
"foo",
"bar",
"hoge"
],
"collectionClass" : "java.util.ArrayList"
},
"class" : "net.wrap_trap.monganez.Bar"
}
], "collectionClass" : "java.util.ArrayList" } } }
- encode Map/Iterable/POJO to DBObject.
- decode DBObject to Map/Iterable/POJO.
- show deails.
mvn package -Dmaven.test.skip=true
- Jars: http://wrap-trap.net/maven2/snapshot/net/wrap-trap/monganez/
- Repository URL: http://wrap-trap.net/maven2/snapshot/
<dependency>
<groupId>net.wrap-trap</groupId>
<artifactId>monganez</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<repository>
<id>wrap-trap.net/maven2/snapshot</id>
<name>wrap-trap.net Maven Snapshot Repository</name>
<url>http://wrap-trap.net/maven2/snapshot</url>
</repository>
ASL 2.0