String entityName = entity.getClass().getName(); throw new MappingException(format("Could not map %s with ID: %s in database '%s'", entityName, id, datastore.getDB().getName()), e);
@Test public void testQueryUnmappedData() { getMorphia().map(Class1.class); getDs().ensureIndexes(true); getDs().getDB().getCollection("user").save( new BasicDBObject() .append("@class", Class1.class.getName()) .append("value1", "foo") .append("someMap", new BasicDBObject("someKey", "value"))); Query<Class1> query = getDs().createQuery(Class1.class); query.disableValidation().criteria("someMap.someKey").equal("value"); Class1 retrievedValue = query.find(new FindOptions().limit(1)).next(); Assert.assertNotNull(retrievedValue); Assert.assertEquals("foo", retrievedValue.value1); }
@Test public void testMapping() { getMorphia().map(ClassLevelThree.class); final ClassLevelThree sp = new ClassLevelThree(); //Old way final DBObject wrapObj = getMorphia().toDBObject(sp); //the error points here from the user getDs().getDB().getCollection("testColl").save(wrapObj); //better way getDs().save(sp); }
@Test public void testIndexedField() { getMorphia().map(HasExpiryField.class); getDs().ensureIndexes(); getDs().save(new HasExpiryField()); final DB db = getDs().getDB(); final DBCollection dbCollection = db.getCollection("HasExpiryField"); final List<DBObject> indexes = dbCollection.getIndexInfo(); Assert.assertNotNull(indexes); Assert.assertEquals(2, indexes.size()); DBObject index = null; for (final DBObject candidateIndex : indexes) { if (candidateIndex.containsField("expireAfterSeconds")) { index = candidateIndex; } } Assert.assertNotNull(index); Assert.assertEquals(5, ((Number) index.get("expireAfterSeconds")).intValue()); }
@Test public void testClassAnnotation() { getMorphia().map(ClassAnnotation.class); getDs().ensureIndexes(); getDs().save(new ClassAnnotation()); final DB db = getDs().getDB(); final DBCollection dbCollection = db.getCollection("ClassAnnotation"); final List<DBObject> indexes = dbCollection.getIndexInfo(); Assert.assertNotNull(indexes); Assert.assertEquals(2, indexes.size()); DBObject index = null; for (final DBObject candidateIndex : indexes) { if (candidateIndex.containsField("expireAfterSeconds")) { index = candidateIndex; } } Assert.assertNotNull(index); Assert.assertTrue(index.containsField("expireAfterSeconds")); Assert.assertEquals(5, ((Number) index.get("expireAfterSeconds")).intValue()); }
id = ref; } else { collection = datastore.getDB().getCollection(dbRef.getCollectionName()); id = dbRef.getId();
@Test public void testEnumMapping() { getDs().getDB().dropDatabase(); getMorphia().map(ContainsEnum.class); getDs().save(new ContainsEnum()); Assert.assertEquals(1, getDs().find(ContainsEnum.class).field("foo").equal(Foo.BAR) .count()); Assert.assertEquals(1, getDs().find(ContainsEnum.class).filter("foo", Foo.BAR) .count()); Assert.assertEquals(1, getDs().find(ContainsEnum.class).disableValidation().filter("foo", Foo.BAR) .count()); }
/** * Drops all data in MongoDB on the connected database */ public void dropDatabase() { this.datastore.getDB().dropDatabase(); } }