Search in sources :

Example 1 with RecursiveChild

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());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) RecursiveChild(org.mongodb.morphia.testmodel.RecursiveChild) RecursiveParent(org.mongodb.morphia.testmodel.RecursiveParent) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Example 2 with RecursiveChild

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);
}
Also used : RecursiveChild(org.mongodb.morphia.testmodel.RecursiveChild) RecursiveParent(org.mongodb.morphia.testmodel.RecursiveParent) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 RecursiveChild (org.mongodb.morphia.testmodel.RecursiveChild)2 RecursiveParent (org.mongodb.morphia.testmodel.RecursiveParent)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBCollection (com.mongodb.DBCollection)1 DBObject (com.mongodb.DBObject)1 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)1