@Test // Listing 4-12 public void shouldRemoveACustomer() throws Exception { Customer03 customer = new Customer03("Antony", "Balla", "*****@*****.**"); Address03 address = new Address03("Ritherdon Rd", "London", "8QE", "UK"); customer.setAddress(address); // Persist the object tx.begin(); em.persist(customer); em.persist(address); tx.commit(); assertNotNull(customer.getId()); assertNotNull(address.getId()); // Removes the object tx.begin(); em.remove(customer); em.remove(address); tx.commit(); assertEquals(customer.getFirstName(), "Antony"); assertEquals(address.getCity(), "London"); customer = em.find(Customer03.class, customer.getId()); assertNull(customer); address = em.find(Address03.class, address.getId()); assertNull(address); }
@Test // Listing 4-14 public void shouldPersistACustomerAndThenRefreshIt() throws Exception { Customer03 customer = new Customer03("Antony", "Balla", "*****@*****.**"); Address03 address = new Address03("Ritherdon Rd", "London", "8QE", "UK"); customer.setAddress(address); // Persist the object tx.begin(); em.persist(customer); em.persist(address); tx.commit(); assertNotNull(customer.getId()); assertNotNull(address.getId()); customer = em.find(Customer03.class, customer.getId()); assertNotNull(customer); assertEquals(customer.getFirstName(), "Antony"); customer.setFirstName("New first name"); assertEquals(customer.getFirstName(), "New first name"); em.refresh(customer); assertEquals(customer.getFirstName(), "Antony"); }
@Test public void shouldPersistAnAddress() throws Exception { Address03 address = new Address03("Ritherdon Rd", "London", "8QE", "UK"); // Persist the object tx.begin(); em.persist(address); tx.commit(); assertNotNull(address.getId()); }
@Test // Listing 4-20 public void shouldPersistACustomerAndCascadeToTheAddress() throws Exception { Customer03 customer = new Customer03("Antony", "Balla", "*****@*****.**"); Address03 address = new Address03("Ritherdon Rd", "London", "8QE", "UK"); customer.setAddress(address); // Persist the object tx.begin(); em.persist(customer); tx.commit(); assertNotNull(customer.getId()); assertNotNull(address.getId()); }
@Test // Listing 4-11 public void shouldGetAReferenceToCustomer() throws Exception { Customer03 customer = new Customer03("Antony", "Balla", "*****@*****.**"); Address03 address = new Address03("Ritherdon Rd", "London", "8QE", "UK"); customer.setAddress(address); // Persist the object tx.begin(); em.persist(customer); em.persist(address); tx.commit(); assertNotNull(customer.getId()); assertNotNull(address.getId()); // Clear em.clear(); customer = em.getReference(Customer03.class, customer.getId()); assertNotNull(customer); }