protected Object getControlObject() { Root root = new Root(); root.employees = new ArrayList<Employee>(); root.addresses = new ArrayList<Address>(); root.phones = new ArrayList<PhoneNumber>(); Employee employee = new Employee(); employee.name = "Jane Doe"; employee.contact = new ArrayList(); Address addr = new Address(); addr.id = "1"; addr.street = "123 Abc Street"; addr.zip = "123456"; root.addresses.add(addr); addr = new Address(); addr.id = "2"; addr.street = "321 Cba Street"; addr.zip = "654321"; root.addresses.add(addr); root.employees.add(employee); PhoneNumber phone = new PhoneNumber(); phone.id = "0"; phone.number = "234-4321"; root.phones.add(phone); phone = new PhoneNumber(); phone.id = "1"; phone.number = "123-3456"; employee.contact = phone; root.phones.add(phone); phone = new PhoneNumber(); phone.id = "11"; phone.number = "555-7777"; root.phones.add(phone); return root; }
@Test public void convertsAddressCorrectly() { Address address = new Address(); address.city = "New York"; address.street = "Broadway"; DBObject dbObject = new BasicDBObject(); converter.write(address, dbObject); assertThat(dbObject.get("city").toString(), is("New York")); assertThat(dbObject.get("street").toString(), is("Broadway")); }
@Test public void testOneToOneOnSubclass() { Person p = new Person(); p.name = "Gavin"; Address a = new Address(); a.entityName = "Gavin"; a.zip = "3181"; a.state = "VIC"; a.street = "Karbarook Ave"; p.address = a; Session s = openSession(); Transaction t = s.beginTransaction(); s.persist(p); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); EntityStatistics addressStats = sessionFactory().getStatistics().getEntityStatistics(Address.class.getName()); EntityStatistics mailingAddressStats = sessionFactory().getStatistics().getEntityStatistics("MailingAddress"); p = (Person) s.createQuery("from Person p join fetch p.address left join fetch p.mailingAddress") .uniqueResult(); assertNotNull(p.address); assertNull(p.mailingAddress); s.clear(); assertEquals(addressStats.getFetchCount(), 0); assertEquals(mailingAddressStats.getFetchCount(), 0); p = (Person) s.createQuery("from Person p join fetch p.address").uniqueResult(); assertNotNull(p.address); assertNull(p.mailingAddress); s.clear(); assertEquals(addressStats.getFetchCount(), 0); assertEquals(mailingAddressStats.getFetchCount(), 1); p = (Person) s.createQuery("from Person").uniqueResult(); assertNotNull(p.address); assertNull(p.mailingAddress); s.clear(); assertEquals(addressStats.getFetchCount(), 1); assertEquals(mailingAddressStats.getFetchCount(), 2); p = (Person) s.createQuery("from Entity").uniqueResult(); assertNotNull(p.address); assertNull(p.mailingAddress); s.clear(); assertEquals(addressStats.getFetchCount(), 2); assertEquals(mailingAddressStats.getFetchCount(), 3); // note that in here join fetch is used for the nullable // one-to-one, due to a very special case of default p = (Person) s.get(Person.class, "Gavin"); assertNotNull(p.address); assertNull(p.mailingAddress); s.clear(); assertEquals(addressStats.getFetchCount(), 2); assertEquals(mailingAddressStats.getFetchCount(), 3); p = (Person) s.get(Entity.class, "Gavin"); assertNotNull(p.address); assertNull(p.mailingAddress); s.clear(); assertEquals(addressStats.getFetchCount(), 2); assertEquals(mailingAddressStats.getFetchCount(), 3); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); Org org = new Org(); org.name = "IFA"; Address a2 = new Address(); a2.entityName = "IFA"; a2.zip = "3181"; a2.state = "VIC"; a2.street = "Orrong Rd"; org.addresses.add(a2); s.persist(org); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); org = (Org) s.get(Entity.class, "IFA"); s.clear(); List list = s.createQuery("from Entity e order by e.name").list(); p = (Person) list.get(0); assertNotNull(p.address); assertNull(p.mailingAddress); org = (Org) list.get(1); assertEquals(org.addresses.size(), 1); s.clear(); list = s.createQuery( "from Entity e left join fetch e.address left join fetch e.mailingAddress order by e.name") .list(); p = (Person) list.get(0); org = (Org) list.get(1); assertNotNull(p.address); assertNull(p.mailingAddress); assertEquals(org.addresses.size(), 1); s.delete(p); s.delete(org); t.commit(); s.close(); }
protected Object getControlObject() { Address addr = new Address(); addr.city = "Ottawa"; addr.street = "Main Street"; return addr; }