0

1対1の休止状態マッピングでレコードを更新および削除する例を教えてください。

customer と customeraddress の 2 つのテーブルがあります。次のコードを使用して、両方のテーブルにレコードを追加できます

customerForm.setcustomerAddress(customerAddress);
customerAddress.setCustomer(customerForm);
getEntityManager().persist(customerForm);

今、レコードを削除して更新したいと思います。

@Entity
@Table(name="customers")
public class Customer {
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name="id")
public int getId() {
    return id;
}
@Column(name="customerName")
public String getcustomerName() {
    return customerName;
}
@Column(name="password")
public String getpassword() {
    return password;
}


@OneToOne(mappedBy = "customer", cascade = CascadeType.ALL)  
public CustomerAddess getcustomerAddress() {
    return customerAddress;
}
//rest setter getter.....

}

@Entity
@Table(name="customer_address")
public class CustomerAddess {
@Id
@GeneratedValue(generator = "gen")  
@GenericGenerator(name = "gen", strategy = "foreign", parameters =    @org.hibernate.annotations.Parameter(name = "property", value = "customer"))  
@Column(name="id")
public int getId() {
    return id;
}

@Column(name="street_address")
public String getstreet_address() {
    return street_address;
}
@OneToOne(fetch = FetchType.EAGER)
@PrimaryKeyJoinColumn  
public Customer getCustomer() {
    return customer;
}

//rest setter getter
4

1 に答える 1

0

私はあなたの例を取り上げて、エンティティを永続化/更新/削除する方法の概要を説明する 2 つの簡単なテストスイートを作成しました。

また、エンティティを少し変更しました (アノテーションがゲッター/セッターの上にあるのは好きではないので、フィールドの上に配置し、ゲッター/セッターに 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);
    }
}
于 2013-10-31T11:06:56.683 に答える