Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

how to update and delete a record in hibernate one to one mapping?

Can any one please show me an example to update and delete a record in one to one hibernate mapping?

I have 2 tables customer and customeraddress. I am able to add record in both tables using following code

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

Now I want to delete and update the record.

@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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I took your example and created two simple testsuites that should give you an overview about how you can persist / update / delete entities.

I also modified your entities slightly (I don't like it if the Annotations are above the getters / setters, so I put them above the fields and applied the javabeans conventions for the getters/setters and removed some spelling mistakes). There are some comments that may give you some hints as well.

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...