/** * Creates an entity and populates its state based on the dbObject given. This method is primarily an internal method. Reliance on * this method may break your application in future releases. * * @param <T> type of the entity * @param datastore the Datastore to use when fetching this reference * @param entityClass type to create * @param dbObject the object state to use * @return the newly created and populated entity */ public <T> T fromDBObject(final Datastore datastore, final Class<T> entityClass, final DBObject dbObject) { return fromDBObject(datastore, entityClass, dbObject, mapper.createEntityCache()); }
private void populate(final BasicDBObject jsonObject) { getMorphia().fromDBObject(null, JsonList.class, jsonObject); }
for (int i = 0; i < iterations; i++) { long start = System.nanoTime(); morphia.fromDBObject(ds, Container.class, p, entityCache); long stop = System.nanoTime();
/** * This test is green when {@link MyEntity#valueObject} is annotated with {@code @Property}, as in this case the field is not * serialized * at all. However, the bson encoder would fail to encode the object of type ValueObject (as shown by {@link * #testFullBSONSerialization()}). */ @Test public void testDBObjectSerialization() { final MyEntity entity = new MyEntity(1L, new ValueObject(2L)); final DBObject dbObject = getMorphia().toDBObject(entity); assertEquals(new BasicDBObject("_id", 1L).append("valueObject", 2L), dbObject); assertEquals(entity, getMorphia().fromDBObject(getDs(), MyEntity.class, dbObject)); }
stuff.save(childDbObj); final RecursiveParent parentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject("_id", parentDbObj.get("_id"))), new DefaultEntityCache()); final RecursiveChild childLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject("_id", childDbObj.get("_id"))), stuff.save(getMorphia().toDBObject(childLoaded)); final RecursiveParent finalParentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject("_id", parentDbObj.get("_id"))), new DefaultEntityCache()); final RecursiveChild finalChildLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject("_id", childDbObj.get("_id"))),
/** * This test shows the full serialization, including bson encoding/decoding. */ @Test public void testFullBSONSerialization() { final MyEntity entity = new MyEntity(1L, new ValueObject(2L)); final DBObject dbObject = getMorphia().toDBObject(entity); final byte[] data = new DefaultDBEncoder().encode(dbObject); final DBObject decoded = new DefaultDBDecoder().decode(data, (DBCollection) null); final MyEntity actual = getMorphia().fromDBObject(getDs(), MyEntity.class, decoded); assertEquals(entity, actual); }
articles.save(relatedDbObj); final Article relatedLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject("_id", relatedDbObj.get("_id"))), articles.save(articleDbObj); final Article articleLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne( new BasicDBObject("_id",
Hotel borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache()); agencies.save(agencyDbObj); final TravelAgency agencyLoaded = getMorphia().fromDBObject(getDs(), TravelAgency.class, agencies.findOne(new BasicDBObject("_id", agencyDbObj.get("_id"))), borgLoaded = getMorphia().fromDBObject(getDs(), Hotel.class, hotelDbObj, new DefaultEntityCache()); assertNull(borgLoaded.getAddress()); assertEquals(0, borgLoaded.getPhoneNumbers().size());
final Shape rectangleLoaded = getMorphia().fromDBObject(getDs(), Shape.class, rectangleDbObjLoaded, new DefaultEntityCache()); final ShapeShifter shifterLoaded = getMorphia().fromDBObject(getDs(), ShapeShifter.class, shifterDbObjLoaded, new DefaultEntityCache()); assertNotNull(shifterLoaded);
@Test public void testDbRefMapping() { getMorphia().map(ContainsRef.class).map(Rectangle.class); final DBCollection stuff = getDb().getCollection("stuff"); final DBCollection rectangles = getDb().getCollection("rectangles"); assertTrue("'ne' field should not be persisted!", !getMorphia().getMapper().getMCMap().get(ContainsRef.class.getName()) .containsJavaFieldName("ne")); final Rectangle r = new Rectangle(1, 1); final DBObject rDbObject = getMorphia().toDBObject(r); rDbObject.put("_ns", rectangles.getName()); rectangles.save(rDbObject); final ContainsRef cRef = new ContainsRef(); cRef.rect = new DBRef((String) rDbObject.get("_ns"), rDbObject.get("_id")); final DBObject cRefDbObject = getMorphia().toDBObject(cRef); stuff.save(cRefDbObject); final BasicDBObject cRefDbObjectLoaded = (BasicDBObject) stuff.findOne(BasicDBObjectBuilder.start("_id", cRefDbObject.get("_id")) .get()); final ContainsRef cRefLoaded = getMorphia().fromDBObject(getDs(), ContainsRef.class, cRefDbObjectLoaded, new DefaultEntityCache()); assertNotNull(cRefLoaded); assertNotNull(cRefLoaded.rect); assertNotNull(cRefLoaded.rect.getId()); assertNotNull(cRefLoaded.rect.getCollectionName()); assertEquals(cRefLoaded.rect.getId(), cRef.rect.getId()); assertEquals(cRefLoaded.rect.getCollectionName(), cRef.rect.getCollectionName()); }