我举了你的例子并创建了两个简单的测试套件,它们应该为你提供有关如何持久化/更新/删除实体的概述。
我还稍微修改了您的实体(如果注释在 getter/setter 之上,我不喜欢它,所以我将它们放在字段之上,并为 getter/setter 应用了 javabeans 约定,并删除了一些拼写错误)。有一些评论也可能会给你一些提示。
CascadeAllTests:
@OneToOne(mappedBy = "customer", cascade = CascadeType.ALL)
CustomerAddress customerAddress;
public class CascadeAllTests {
private EntityManager em;
@Before
public void before() {
final EntityManagerFactory emf = Persistence
.createEntityManagerFactory("pu");
em = emf.createEntityManager();
}
@After
public void after() {
em.close();
em = null;
}
@Test
public void persist() {
Customer c = new Customer();
c.setCustomerName("cName");
c.setPassword("cPass");
CustomerAddress addr = new CustomerAddress();
addr.setCustomer(c);
addr.setStreetAddress("Addr");
c.setCustomerAddress(addr);
em.getTransaction().begin();
em.persist(c); // it is not necessary to call em.persist(addr)
em.getTransaction().commit();
Customer cStored = em.find(Customer.class, c.getId());
assertNotNull(cStored);
assertEquals(c.getCustomerName(), cStored.getCustomerName());
assertEquals(addr.getStreetAddress(), cStored.getCustomerAddress()
.getStreetAddress());
}
@Test
public void update() {
Customer c = new Customer();
c.setCustomerName("cName");
c.setPassword("cPass");
CustomerAddress addr = new CustomerAddress();
addr.setCustomer(c);
addr.setStreetAddress("Addr");
c.setCustomerAddress(addr);
em.getTransaction().begin();
em.persist(c);
em.getTransaction().commit();
em.detach(c); // c and c.customerAddress are detached now.
addr.setStreetAddress("New address");
c.setCustomerName("New Name");
em.getTransaction().begin();
c = em.merge(c);
em.getTransaction().commit();
// due to CascadeType.ALL on Customer.customerAddress the new address is
// stored now
Customer cStored = em.find(Customer.class, c.getId());
assertNotNull(cStored);
assertEquals(c.getCustomerName(), cStored.getCustomerName());
assertEquals(addr.getStreetAddress(), cStored.getCustomerAddress()
.getStreetAddress());
}
@Test
public void deleteAddressOnly() {
Customer c = new Customer();
c.setCustomerName("cName");
c.setPassword("cPass");
CustomerAddress addr = new CustomerAddress();
addr.setCustomer(c);
addr.setStreetAddress("Addr");
c.setCustomerAddress(addr);
em.getTransaction().begin();
em.persist(c);
em.getTransaction().commit();
em.detach(c); // c and c.customerAddress are detached now
em.getTransaction().begin();
c = em.merge(c); // c and c.customerAddress are attached again
em.remove(c.getCustomerAddress());
c.setCustomerAddress(null);
em.getTransaction().commit();
// due to CascadeType.ALL on Customer.customerAddress the new address is
// stored now
Customer cStored = em.find(Customer.class, c.getId());
CustomerAddress addrStored = em.find(CustomerAddress.class,
addr.getId());
assertNotNull(cStored);
assertEquals(c.getCustomerName(), cStored.getCustomerName());
assertNull(cStored.getCustomerAddress());
assertNull(addrStored);
}
}
CascadeNoneTests:
@OneToOne(mappedBy = "customer", cascade = {})
CustomerAddress customerAddress;
public class CascadeNonTests {
private EntityManager em;
@Before
public void before() {
final EntityManagerFactory emf = Persistence
.createEntityManagerFactory("pu");
em = emf.createEntityManager();
}
@After
public void after() {
em.close();
em = null;
}
@Test
public void persist() {
Customer c = new Customer();
c.setCustomerName("cName");
c.setPassword("cPass");
CustomerAddress addr = new CustomerAddress();
addr.setCustomer(c);
addr.setStreetAddress("Addr");
c.setCustomerAddress(addr);
em.getTransaction().begin();
em.persist(c);
em.persist(addr); // has to be called explicitly
em.getTransaction().commit();
Customer cStored = em.find(Customer.class, c.getId());
assertNotNull(cStored);
assertEquals(c.getCustomerName(), cStored.getCustomerName());
assertEquals(addr.getStreetAddress(), cStored.getCustomerAddress()
.getStreetAddress());
}
@Test
public void update() {
Customer c = new Customer();
c.setCustomerName("cName");
c.setPassword("cPass");
CustomerAddress addr = new CustomerAddress();
addr.setCustomer(c);
addr.setStreetAddress("Addr");
c.setCustomerAddress(addr);
em.getTransaction().begin();
em.persist(c);
em.persist(addr);// has to be called explicitly
em.getTransaction().commit();
em.detach(c);
em.detach(addr); // c and c.customerAddress are detached now.
addr.setStreetAddress("New address");
c.setCustomerName("New Name");
em.getTransaction().begin();
c = em.merge(c);
addr = em.merge(addr);
em.getTransaction().commit();
Customer cStored = em.find(Customer.class, c.getId());
assertNotNull(cStored);
assertEquals(c.getCustomerName(), cStored.getCustomerName());
assertEquals(addr.getStreetAddress(), cStored.getCustomerAddress()
.getStreetAddress());
}
@Test
public void deleteAddressOnly() {
Customer c = new Customer();
c.setCustomerName("cName");
c.setPassword("cPass");
CustomerAddress addr = new CustomerAddress();
addr.setCustomer(c);
addr.setStreetAddress("Addr");
c.setCustomerAddress(addr);
em.getTransaction().begin();
em.persist(c);
em.persist(addr);// has to be called explicitly
em.getTransaction().commit();
em.detach(c);
em.detach(addr); // c and c.customerAddress are detached now.
em.getTransaction().begin();
c = em.merge(c);
addr = em.merge(addr);
em.remove(c.getCustomerAddress());
c.setCustomerAddress(null);
em.getTransaction().commit();
Customer cStored = em.find(Customer.class, c.getId());
CustomerAddress addrStored = em.find(CustomerAddress.class,
addr.getId());
assertNotNull(cStored);
assertEquals(c.getCustomerName(), cStored.getCustomerName());
assertNull(cStored.getCustomerAddress());
assertNull(addrStored);
}
}