Example #1
1
  @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"));
  }
Example #2
0
  @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();
    }
  }
Example #3
0
 protected Person makePerson() {
   Person person = new Person();
   person.setFirstName("joe");
   person.setLastName("smith");
   person.setSsn("123");
   Address address = new Address(0, "street1", "city1", "state1", "zip1");
   person.getAddresses().add(address);
   return person;
 }
Example #4
0
  @Test
  public void testDTOs() throws Exception {
    log.info("*** testDTOs ***");

    for (int i = 0; i < 10; i++) {
      Person person = makePerson();
      person.setLastName("smith" + i);
      registrar.createPerson(person);
    }

    // now get a graph of objects that contain pure DTOs versus BOs
    Collection<PersonDTO> peopleDTO = registrar.getPeopleByNameDTO("joe", "%");
    assertEquals("unexpected number of DTO people", 10, peopleDTO.size());
    for (PersonDTO p : peopleDTO) {
      Collection<AddressDTO> a = p.getAddresses();
      a.iterator().next().getZip();
    }
    // the DTOs are POJOs that are designed to only contain what the
    // clients needs to see. It contains no server-side behavior and
    // could be represented as an XML document. In this example, we
    // have excluded the SSN from the PersonDTO.
  }