Search in sources :

Example 1 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.

the class TestInterfaces method testDynamicInstantiation.

@Test
public void testDynamicInstantiation() throws Exception {
    final DBCollection shapes = getDb().getCollection("shapes");
    final DBCollection shapeshifters = getDb().getCollection("shapeshifters");
    getMorphia().map(Circle.class).map(Rectangle.class).map(ShapeShifter.class);
    final Shape rectangle = new Rectangle(2, 5);
    final DBObject rectangleDbObj = getMorphia().toDBObject(rectangle);
    shapes.save(rectangleDbObj);
    final BasicDBObject rectangleDbObjLoaded = (BasicDBObject) shapes.findOne(new BasicDBObject(Mapper.ID_KEY, rectangleDbObj.get(Mapper.ID_KEY)));
    final Shape rectangleLoaded = getMorphia().fromDBObject(getDs(), Shape.class, rectangleDbObjLoaded, new DefaultEntityCache());
    assertTrue(rectangle.getArea() == rectangleLoaded.getArea());
    assertTrue(rectangleLoaded instanceof Rectangle);
    final ShapeShifter shifter = new ShapeShifter();
    shifter.setReferencedShape(rectangleLoaded);
    shifter.setMainShape(new Circle(2.2));
    shifter.getAvailableShapes().add(new Rectangle(3, 3));
    shifter.getAvailableShapes().add(new Circle(4.4));
    final DBObject shifterDbObj = getMorphia().toDBObject(shifter);
    shapeshifters.save(shifterDbObj);
    final BasicDBObject shifterDbObjLoaded = (BasicDBObject) shapeshifters.findOne(new BasicDBObject(Mapper.ID_KEY, shifterDbObj.get(Mapper.ID_KEY)));
    final ShapeShifter shifterLoaded = getMorphia().fromDBObject(getDs(), ShapeShifter.class, shifterDbObjLoaded, new DefaultEntityCache());
    assertNotNull(shifterLoaded);
    assertNotNull(shifterLoaded.getReferencedShape());
    assertNotNull(shifterLoaded.getReferencedShape().getArea());
    assertNotNull(rectangle);
    assertNotNull(rectangle.getArea());
    assertTrue(rectangle.getArea() == shifterLoaded.getReferencedShape().getArea());
    assertTrue(shifterLoaded.getReferencedShape() instanceof Rectangle);
    assertTrue(shifter.getMainShape().getArea() == shifterLoaded.getMainShape().getArea());
    assertEquals(shifter.getAvailableShapes().size(), shifterLoaded.getAvailableShapes().size());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Circle(org.mongodb.morphia.testmodel.Circle) Shape(org.mongodb.morphia.testmodel.Shape) Rectangle(org.mongodb.morphia.testmodel.Rectangle) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) ShapeShifter(org.mongodb.morphia.testmodel.ShapeShifter) Test(org.junit.Test)

Example 2 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.

the class TestMapping method testMaps.

@Test
public void testMaps() throws Exception {
    final DBCollection articles = getDb().getCollection("articles");
    getMorphia().map(Article.class).map(Translation.class).map(Circle.class);
    final Article related = new Article();
    final BasicDBObject relatedDbObj = (BasicDBObject) getMorphia().toDBObject(related);
    articles.save(relatedDbObj);
    final Article relatedLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, relatedDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    final Article article = new Article();
    article.setTranslation("en", new Translation("Hello World", "Just a test"));
    article.setTranslation("is", new Translation("Halló heimur", "Bara að prófa"));
    article.setAttribute("myDate", new Date());
    article.setAttribute("myString", "Test");
    article.setAttribute("myInt", 123);
    article.putRelated("test", relatedLoaded);
    final BasicDBObject articleDbObj = (BasicDBObject) getMorphia().toDBObject(article);
    articles.save(articleDbObj);
    final Article articleLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, articleDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
    assertEquals(article.getTranslations().size(), articleLoaded.getTranslations().size());
    assertEquals(article.getTranslation("en").getTitle(), articleLoaded.getTranslation("en").getTitle());
    assertEquals(article.getTranslation("is").getBody(), articleLoaded.getTranslation("is").getBody());
    assertEquals(article.getAttributes().size(), articleLoaded.getAttributes().size());
    assertEquals(article.getAttribute("myDate"), articleLoaded.getAttribute("myDate"));
    assertEquals(article.getAttribute("myString"), articleLoaded.getAttribute("myString"));
    assertEquals(article.getAttribute("myInt"), articleLoaded.getAttribute("myInt"));
    assertEquals(article.getRelated().size(), articleLoaded.getRelated().size());
    assertEquals(article.getRelated("test").getId(), articleLoaded.getRelated("test").getId());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Translation(org.mongodb.morphia.testmodel.Translation) Article(org.mongodb.morphia.testmodel.Article) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Date(java.util.Date) Test(org.junit.Test)

Example 3 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache 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 4 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.

the class TestAsListPerf method driverQueryAndMorphiaConverter.

public double driverQueryAndMorphiaConverter(final int nbOfHits) {
    final long start = System.nanoTime();
    final List<DBObject> list = getDs().getDB().getCollection("Address").find().sort(new BasicDBObject("name", 1)).toArray();
    final EntityCache entityCache = new DefaultEntityCache();
    final List<Address> resultList = new LinkedList<Address>();
    for (final DBObject dbObject : list) {
        final Address address = getMorphia().fromDBObject(getDs(), Address.class, dbObject, entityCache);
        resultList.add(address);
    }
    //ns -> ms
    final long duration = (System.nanoTime() - start) / 1000000;
    Assert.assertEquals(nbOfHits, resultList.size());
    return (double) duration / nbOfHits;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) EntityCache(org.mongodb.morphia.mapping.cache.EntityCache) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) LinkedList(java.util.LinkedList)

Example 5 with DefaultEntityCache

use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.

the class TestMapping method testMapping.

@Test
public void testMapping() {
    final BasicDAO<User, ObjectId> messageDAO = new BasicDAO<User, ObjectId>(User.class, getDs());
    Assert.assertNotNull(messageDAO);
    Mapper mapper = new Mapper();
    User user = new User();
    user.id = 1;
    user.userObject = "just a String";
    DBObject dbObject = mapper.toDBObject(user);
    Object object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
    Assert.assertEquals(user.userObject, ((User) object).userObject);
    user.userObject = 33;
    dbObject = mapper.toDBObject(user);
    object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
    Assert.assertEquals(user.userObject, ((User) object).userObject);
    user.userObject = 33.3;
    dbObject = mapper.toDBObject(user);
    object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
    Assert.assertEquals(user.userObject, ((User) object).userObject);
}
Also used : Mapper(org.mongodb.morphia.mapping.Mapper) ObjectId(org.bson.types.ObjectId) BasicDAO(org.mongodb.morphia.dao.BasicDAO) DBObject(com.mongodb.DBObject) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) DBObject(com.mongodb.DBObject) Test(org.junit.Test)

Aggregations

DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)9 BasicDBObject (com.mongodb.BasicDBObject)7 Test (org.junit.Test)7 DBObject (com.mongodb.DBObject)6 DBCollection (com.mongodb.DBCollection)5 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 Mapper (org.mongodb.morphia.mapping.Mapper)2 Rectangle (org.mongodb.morphia.testmodel.Rectangle)2 BasicDBList (com.mongodb.BasicDBList)1 DBRef (com.mongodb.DBRef)1 Arrays.asList (java.util.Arrays.asList)1 LinkedList (java.util.LinkedList)1 Vector (java.util.Vector)1 ObjectId (org.bson.types.ObjectId)1 BasicDAO (org.mongodb.morphia.dao.BasicDAO)1 EntityCache (org.mongodb.morphia.mapping.cache.EntityCache)1 Address (org.mongodb.morphia.testmodel.Address)1 Article (org.mongodb.morphia.testmodel.Article)1