use of org.mongodb.morphia.testmodel.RecursiveChild in project morphia by mongodb.
the class TestMapping method testRecursiveReference.
@Test
public void testRecursiveReference() throws Exception {
final DBCollection stuff = getDb().getCollection("stuff");
getMorphia().map(RecursiveParent.class).map(RecursiveChild.class);
final RecursiveParent parent = new RecursiveParent();
final DBObject parentDbObj = getMorphia().toDBObject(parent);
stuff.save(parentDbObj);
final RecursiveChild child = new RecursiveChild();
final DBObject childDbObj = getMorphia().toDBObject(child);
stuff.save(childDbObj);
final RecursiveParent parentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
final RecursiveChild childLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
parentLoaded.setChild(childLoaded);
childLoaded.setParent(parentLoaded);
stuff.save(getMorphia().toDBObject(parentLoaded));
stuff.save(getMorphia().toDBObject(childLoaded));
final RecursiveParent finalParentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
final RecursiveChild finalChildLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
assertNotNull(finalParentLoaded.getChild());
assertNotNull(finalChildLoaded.getParent());
}
use of org.mongodb.morphia.testmodel.RecursiveChild in project morphia by mongodb.
the class TestMapping method testReferenceWithoutIdValue.
@Test(expected = MappingException.class)
public void testReferenceWithoutIdValue() throws Exception {
final RecursiveParent parent = new RecursiveParent();
final RecursiveChild child = new RecursiveChild();
child.setId(null);
parent.setChild(child);
getDs().save(parent);
}
Aggregations