use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method testInsertWithRef.
@Test
public void testInsertWithRef() {
final Pic pic = new Pic();
pic.setName("fist");
final ObjectId picKey = getDs().save(pic).getId();
Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", picKey));
assertInserted(query.update(set("name", "A")).execute(new UpdateOptions().upsert(true)));
MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
getDs().find(ContainsPic.class).delete(new DeleteOptions().multi(true));
query = getDs().find(ContainsPic.class).filter(eq("name", "first"), eq("pic", pic));
assertInserted(query.update(set("name", "second")).execute(new UpdateOptions().upsert(true)));
MatcherAssert.assertThat(getDs().find(ContainsPic.class).count(), is(1L));
// test reading the object.
final ContainsPic cp = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
assertThat(cp, is(notNullValue()));
MatcherAssert.assertThat(cp.getName(), is("second"));
MatcherAssert.assertThat(cp.getPic(), is(notNullValue()));
MatcherAssert.assertThat(cp.getPic().getName(), is(notNullValue()));
MatcherAssert.assertThat(cp.getPic().getName(), is("fist"));
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method retainsClassName.
@Test
public void retainsClassName() {
final MapsOfStuff mapsOfStuff = new MapsOfStuff();
final Stuff1 stuff1 = new Stuff1();
stuff1.foo = "narf";
mapsOfStuff.map.put("k1", stuff1);
final Stuff2 stuff2 = new Stuff2();
stuff2.bar = "blarg";
mapsOfStuff.map.put("k2", stuff2);
getDs().save(mapsOfStuff);
final Query<MapsOfStuff> query = getDs().find(MapsOfStuff.class);
query.update(set("map.k2", stuff1)).execute();
// fails due to type now missing
getDs().find(MapsOfStuff.class).iterator(new FindOptions().limit(1)).next();
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestUpdateOperations method testUpdateRef.
@Test
public void testUpdateRef() {
final ContainsPic cp = new ContainsPic();
cp.setName("cp one");
getDs().save(cp);
final Pic pic = new Pic();
pic.setName("fist");
getDs().save(pic);
Query<ContainsPic> query = getDs().find(ContainsPic.class).filter(eq("name", cp.getName()));
UpdateResult result = query.update(set("pic", pic)).execute();
Assert.assertEquals(result.getModifiedCount(), 1);
// test reading the object.
final ContainsPic cp2 = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
assertThat(cp2, is(notNullValue()));
MatcherAssert.assertThat(cp.getName(), CoreMatchers.is(cp2.getName()));
MatcherAssert.assertThat(cp2.getPic(), is(notNullValue()));
MatcherAssert.assertThat(cp2.getPic().getName(), is(notNullValue()));
MatcherAssert.assertThat(pic.getName(), CoreMatchers.is(cp2.getPic().getName()));
// test reading the object.
final ContainsPic cp3 = getDs().find(ContainsPic.class).iterator(new FindOptions().limit(1)).next();
assertThat(cp3, is(notNullValue()));
MatcherAssert.assertThat(cp.getName(), CoreMatchers.is(cp3.getName()));
MatcherAssert.assertThat(cp3.getPic(), is(notNullValue()));
MatcherAssert.assertThat(cp3.getPic().getName(), is(notNullValue()));
MatcherAssert.assertThat(pic.getName(), CoreMatchers.is(cp3.getPic().getName()));
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestGenerics method example.
@Test
public void example() {
ChildEntity entity = new ChildEntity();
entity.setEmbeddedList(asList(new Child("first"), new Child("second"), new Another("third")));
getDs().save(entity);
ChildEntity childEntity = getDs().find(ChildEntity.class).iterator(new FindOptions().limit(1)).next();
Assert.assertEquals(childEntity, entity);
}
use of dev.morphia.query.FindOptions in project morphia by mongodb.
the class TestMapping method testLoadOnly.
@Test
public void testLoadOnly() {
getDs().save(new Normal("value"));
Normal n = getDs().find(Normal.class).iterator(new FindOptions().limit(1)).next();
assertNotNull(n);
assertNotNull(n.name);
getDs().delete(n);
getDs().save(new NormalWithLoadOnly());
n = getDs().find(Normal.class).iterator(new FindOptions().limit(1)).next();
assertNotNull(n);
assertNull(n.name);
getDs().delete(n);
getDs().save(new Normal("value21"));
final NormalWithLoadOnly notSaved = getDs().find(NormalWithLoadOnly.class).iterator(new FindOptions().limit(1)).next();
assertNotNull(notSaved);
assertNotNull(notSaved.name);
assertEquals(notSaved.name, "never");
}
Aggregations