@Test public void testPOJO() throws Exception { log.info("*** testPOJO ***"); for (int i = 0; i < 10; i++) { Person person = makePerson(); person.setLastName("smith" + i); registrar.createPerson(person); } // the objects returned will be fully loaded, but... Collection<Person> people = registrar.getPeopleByNameHydrated("joe", "%"); assertEquals("unexpected number of managed people", 10, people.size()); // the collection class requires hibernate to be in the path Class<?> clazz = people.iterator().next().getAddresses().getClass(); log.debug("collection class=" + clazz); assertTrue("unexpected collection class", clazz.getPackage().getName().contains("hibernate")); // now get a graph of objects that contain pure POJO classes. The // server will create fresh POJOs for DTOs and pass information from // the business object POJO to the data transfer object POJO. people = registrar.getPeopleByNameCleaned("joe", "%"); assertEquals("unexpected number of clean people", 10, people.size()); for (Person p : people) { p.getAddresses().iterator().next().getZip(); } // the POJOs are cleansed of their hibernate types clazz = people.iterator().next().getAddresses().getClass(); log.debug("collection class=" + clazz); assertFalse("unexpected collection class", clazz.getPackage().getName().contains("hibernate")); }
@Test public void testLazy() throws Exception { log.info("*** testLazy ***"); for (int i = 0; i < 10; i++) { Person person = makePerson(); person.setLastName("smith" + i); registrar.createPerson(person); } // the first time we are going to get people straight from the DAO, // without cleaning the managed object or creating a new DTO. Collection<Person> people = registrar.getPeopleByName("joe", "%"); assertEquals("unexpected number of lazy people", 10, people.size()); try { for (Person p : people) { p.getAddresses().iterator().next().getZip(); } fail("no lazy instantiation exception thrown"); } catch (LazyInitializationException expected) { log.info("got expected lazy instantiation exception:" + expected); } // this time, the EJB will be asked to walk the graph returned people = registrar.getPeopleByNameHydrated("joe", "%"); assertEquals("unexpected number of loaded people", 10, people.size()); for (Person p : people) { p.getAddresses().iterator().next().getZip(); } }