Search in sources :

Example 1 with BasicDAO

use of org.mongodb.morphia.dao.BasicDAO in project morphia by mongodb.

the class TestDAO method testNewDAO.

@Test
public void testNewDAO() throws Exception {
    getMorphia().map(Hotel.class);
    final DAO<Hotel, ObjectId> hotelDAO = new BasicDAO<Hotel, ObjectId>(Hotel.class, getMongoClient(), getMorphia(), "morphia_test");
    final Hotel borg = new Hotel();
    borg.setName("Hotel Borg");
    borg.setStars(3);
    borg.setTakesCreditCards(true);
    borg.setStartDate(new Date());
    borg.setType(Hotel.Type.LEISURE);
    final Address address = new Address();
    address.setStreet("Posthusstraeti 11");
    address.setPostCode("101");
    borg.setAddress(address);
    hotelDAO.deleteByQuery((Query<Hotel>) hotelDAO.find());
    hotelDAO.save(borg);
    assertEquals(1, hotelDAO.count());
    assertNotNull(borg.getId());
    final Hotel hotelLoaded = hotelDAO.get(borg.getId());
    assertEquals(borg.getName(), hotelLoaded.getName());
    assertEquals(borg.getAddress().getPostCode(), hotelLoaded.getAddress().getPostCode());
    final Hotel hotelByValue = hotelDAO.findOne("name", "Hotel Borg");
    assertNotNull(hotelByValue);
    assertEquals(borg.getStartDate(), hotelByValue.getStartDate());
    assertTrue(hotelDAO.exists("stars", 3));
    final Hotel hilton = new Hotel();
    hilton.setName("Hilton Hotel");
    hilton.setStars(4);
    hilton.setTakesCreditCards(true);
    hilton.setStartDate(new Date());
    hilton.setType(Hotel.Type.BUSINESS);
    final Address hiltonAddress = new Address();
    hiltonAddress.setStreet("Some street 44");
    hiltonAddress.setPostCode("101");
    hilton.setAddress(hiltonAddress);
    hilton.getPhoneNumbers().add(new PhoneNumber(354, 1234567, PhoneNumber.Type.PHONE));
    hotelDAO.save(hilton);
    assertEquals(2, hotelDAO.find().asList().size());
    assertEquals(2, hotelDAO.findIds().size());
    List<ObjectId> names = hotelDAO.findIds("name", hilton.getName());
    assertEquals(1, names.size());
    assertEquals(hilton.getId(), names.get(0));
    List<ObjectId> stars = hotelDAO.findIds(getDs().find(Hotel.class).field("stars").equal(4));
    assertEquals(1, stars.size());
    assertEquals(hilton.getId(), stars.get(0));
    assertEquals(borg.getId(), hotelDAO.findOneId().getId());
    assertEquals(hilton.getId(), hotelDAO.findOneId("name", hilton.getName()).getId());
    assertEquals(hilton.getId(), hotelDAO.findOneId(getDs().find(Hotel.class).field("stars").equal(4)).getId());
    assertEquals(1, hotelDAO.createQuery().asList(new FindOptions().skip(1).limit(10)).size());
    assertEquals(1, hotelDAO.createQuery().asList(new FindOptions().limit(1)).size());
    assertTrue(hotelDAO.exists("type", Hotel.Type.BUSINESS));
    assertNotNull(hotelDAO.findOne("type", Hotel.Type.LEISURE));
    assertEquals(1, hotelDAO.count(hotelDAO.createQuery().field("stars").notEqual(4)));
    assertEquals(2, hotelDAO.count(hotelDAO.createQuery().field("stars").lessThan(5)));
    assertEquals(1, hotelDAO.count(hotelDAO.createQuery().field("stars").greaterThanOrEq(4)));
    assertEquals(2, hotelDAO.count(hotelDAO.createQuery().field("stars").lessThan(5)));
    assertEquals(1, hotelDAO.count(hotelDAO.createQuery().field("phoneNumbers").sizeEq(1)));
    assertEquals(1, hotelDAO.count(hotelDAO.createQuery().filter("stars", 4).order("address.address_street")));
    assertEquals(hilton.getName(), hotelDAO.find(hotelDAO.createQuery().filter("stars", 4).order("address.address_street")).iterator().next().getName());
    assertEquals(hilton.getName(), hotelDAO.find(hotelDAO.createQuery().filter("stars", 4).order("-address.address_street")).iterator().next().getName());
    assertEquals(hilton.getName(), hotelDAO.find(hotelDAO.createQuery().filter("stars", 4).order("stars, -address.address_street")).iterator().next().getName());
    hotelDAO.deleteById(borg.getId());
    assertEquals(1, hotelDAO.count());
    hotelDAO.getCollection().drop();
    assertEquals(0, hotelDAO.count());
}
Also used : FindOptions(org.mongodb.morphia.query.FindOptions) Address(org.mongodb.morphia.testmodel.Address) ObjectId(org.bson.types.ObjectId) BasicDAO(org.mongodb.morphia.dao.BasicDAO) PhoneNumber(org.mongodb.morphia.testmodel.PhoneNumber) Hotel(org.mongodb.morphia.testmodel.Hotel) Date(java.util.Date) Test(org.junit.Test)

Example 2 with BasicDAO

use of org.mongodb.morphia.dao.BasicDAO 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

ObjectId (org.bson.types.ObjectId)2 Test (org.junit.Test)2 BasicDAO (org.mongodb.morphia.dao.BasicDAO)2 DBObject (com.mongodb.DBObject)1 Date (java.util.Date)1 Mapper (org.mongodb.morphia.mapping.Mapper)1 DefaultEntityCache (org.mongodb.morphia.mapping.cache.DefaultEntityCache)1 FindOptions (org.mongodb.morphia.query.FindOptions)1 Address (org.mongodb.morphia.testmodel.Address)1 Hotel (org.mongodb.morphia.testmodel.Hotel)1 PhoneNumber (org.mongodb.morphia.testmodel.PhoneNumber)1