Search in sources :

Example 1 with UsesCustomIdObject

use of dev.morphia.test.models.UsesCustomIdObject in project morphia by mongodb.

the class TestQuery method testQBE.

@Test
public void testQBE() {
    final CustomId cId = new CustomId();
    cId.setId(new ObjectId());
    cId.setType("banker");
    final UsesCustomIdObject object = new UsesCustomIdObject();
    object.setId(cId);
    object.setText("hllo");
    getDs().save(object);
    final UsesCustomIdObject loaded;
    // Add back if/when query by example for embedded fields is supported (require dotting each field).
    // CustomId exId = new CustomId();
    // exId.type = cId.type;
    // loaded = getDs().find(UsesCustomIdObject.class, "_id", exId).get();
    // assertNotNull(loaded);
    final UsesCustomIdObject ex = new UsesCustomIdObject();
    ex.setText(object.getText());
    loaded = getDs().queryByExample(ex).iterator(new FindOptions().limit(1)).next();
    assertNotNull(loaded);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ObjectId(org.bson.types.ObjectId) CustomId(dev.morphia.test.models.CustomId) UsesCustomIdObject(dev.morphia.test.models.UsesCustomIdObject) Test(org.testng.annotations.Test)

Example 2 with UsesCustomIdObject

use of dev.morphia.test.models.UsesCustomIdObject in project morphia by mongodb.

the class TestQuery method testComplexIdQueryWithRenamedField.

@Test
public void testComplexIdQueryWithRenamedField() {
    final CustomId cId = new CustomId();
    cId.setId(new ObjectId());
    cId.setType("banker");
    final UsesCustomIdObject object = new UsesCustomIdObject();
    object.setId(cId);
    object.setText("hllo");
    getDs().save(object);
    assertNotNull(getDs().find(UsesCustomIdObject.class).filter(eq("_id.t", "banker")).iterator(new FindOptions().limit(1)).tryNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) ObjectId(org.bson.types.ObjectId) CustomId(dev.morphia.test.models.CustomId) UsesCustomIdObject(dev.morphia.test.models.UsesCustomIdObject) Test(org.testng.annotations.Test)

Example 3 with UsesCustomIdObject

use of dev.morphia.test.models.UsesCustomIdObject in project morphia by mongodb.

the class TestQuery method testKeys.

@Test
public void testKeys() {
    PhotoWithKeywords pwk1 = new PhotoWithKeywords("california", "nevada", "arizona");
    PhotoWithKeywords pwk2 = new PhotoWithKeywords("Joe", "Sarah");
    PhotoWithKeywords pwk3 = new PhotoWithKeywords("MongoDB", "World");
    getDs().save(asList(pwk1, pwk2, pwk3));
    MongoCursor<Key<PhotoWithKeywords>> keys = getDs().find(PhotoWithKeywords.class).keys();
    assertTrue(keys.hasNext());
    assertEquals(pwk1.id, keys.next().getId());
    assertEquals(pwk2.id, keys.next().getId());
    assertEquals(pwk3.id, keys.next().getId());
    List<UsesCustomIdObject> list = asList(new UsesCustomIdObject(new CustomId("Turk"), "Turk"), new UsesCustomIdObject(new CustomId("JD"), "Dorian"), new UsesCustomIdObject(new CustomId("Carla"), "Espinosa"));
    getDs().save(list);
    Iterator<Key<UsesCustomIdObject>> complexKeys = getDs().find(UsesCustomIdObject.class).keys();
    assertTrue(complexKeys.hasNext());
    assertEquals(list.get(0).getId(), complexKeys.next().getId());
    assertEquals(list.get(1).getId(), complexKeys.next().getId());
    assertEquals(list.get(2).getId(), complexKeys.next().getId());
    assertFalse(complexKeys.hasNext());
}
Also used : CustomId(dev.morphia.test.models.CustomId) UsesCustomIdObject(dev.morphia.test.models.UsesCustomIdObject) Key(dev.morphia.Key) Test(org.testng.annotations.Test)

Example 4 with UsesCustomIdObject

use of dev.morphia.test.models.UsesCustomIdObject in project morphia by mongodb.

the class TestQuery method testComplexIdQuery.

@Test
public void testComplexIdQuery() {
    final CustomId cId = new CustomId();
    cId.setId(new ObjectId());
    cId.setType("banker");
    final UsesCustomIdObject object = new UsesCustomIdObject();
    object.setId(cId);
    object.setText("hllo");
    getDs().save(object);
    assertNotNull(getDs().find(UsesCustomIdObject.class).filter(eq("_id.type", "banker")).iterator(new FindOptions().limit(1)).tryNext());
    assertNotNull(getDs().find(UsesCustomIdObject.class).filter(in("_id", singletonList(cId))).iterator(new FindOptions().limit(1)).tryNext());
}
Also used : FindOptions(dev.morphia.query.FindOptions) ObjectId(org.bson.types.ObjectId) CustomId(dev.morphia.test.models.CustomId) UsesCustomIdObject(dev.morphia.test.models.UsesCustomIdObject) Test(org.testng.annotations.Test)

Aggregations

CustomId (dev.morphia.test.models.CustomId)4 UsesCustomIdObject (dev.morphia.test.models.UsesCustomIdObject)4 Test (org.testng.annotations.Test)4 FindOptions (dev.morphia.query.FindOptions)3 ObjectId (org.bson.types.ObjectId)3 Key (dev.morphia.Key)1