package com.jpa.yingshe; import javax.persistence.*; import java.util.HashSet; import java.util.Set; @Table(name = "JPA_CUTOMERS") @Entity public class Customer { private Integer id; private String lastName; private String email; private Set<Order> orders = new HashSet<>(); public Customer() {} public Customer(String lastName) { this.lastName = lastName; } @GeneratedValue(strategy = GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "LAST_NAME", length = 50, nullable = false) public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @JoinColumn(name="CUSTOMER_ID") @OneToMany(cascade={CascadeType.REMOVE}) public Set<Order> getOrders() { return orders; } public void setOrders(Set<Order> orders) { this.orders = orders; } }
package com.jpa.yingshe; import javax.persistence.*; @Table(name = "JPA_ORDERS") @Entity public class Order { private Integer id; private String orderName; private Customer customer; @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "ORDER_NAME") public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } @JoinColumn(name="CUSTOMER_ID") @ManyToOne(fetch=FetchType.LAZY) public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
package jpa.test; import com.jpa.yingshe.Customer; import com.jpa.yingshe.Order; import org.junit.After; import org.junit.Before; import org.junit.Test; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class JPAyingshe { private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; private EntityTransaction transaction; @Before public void init() { entityManagerFactory = Persistence.createEntityManagerFactory("jpaname"); entityManager = entityManagerFactory.createEntityManager(); transaction = entityManager.getTransaction(); transaction.begin(); } @After public void destroy() { transaction.commit(); entityManager.close(); entityManagerFactory.close(); } // 若是双向 1-n 的关联关系, 执行保存时 // 若先保存 n 的一端, 再保存 1 的一端, 默认情况下, 会多出 2n 条 UPDATE 语句 // 若先保存 1 的一端, 则会多出 n 条 UPDATE 语句 // 在进行双向 1-n 关联关系时, 建议使用 n 的一方来维护关联关系, 而 1 的一方不维护关联系, 这样会有效的减少 SQL 语句 // 注意: 若在 1 的一端的 @OneToMany 中使用 mappedBy 属性, 则 @OneToMany 端就不能再使用 @JoinColumn 属性了 @Test public void testOneToManyPersist() { Customer customer = new Customer(); customer.setEmail("mm@163.com"); customer.setLastName("MM"); Order order1 = new Order(); order1.setOrderName("O-MM-1"); Order order2 = new Order(); order2.setOrderName("O-MM-2"); //建立关联关系 customer.getOrders().add(order1); customer.getOrders().add(order2); order1.setCustomer(customer); order2.setCustomer(customer); entityManager.persist(order1); entityManager.persist(order2); //执行保存操作 entityManager.persist(customer); } }
由 n 方来维护关联关系,1 的一方不维护
// 若在 1 的一端的 @OneToMany 中使用 mappedBy 属性, 则 @OneToMany 端就不能再使用 @JoinColumn 属性 // @JoinColumn(name="CUSTOMER_ID") @OneToMany(mappedBy="customer") public Set<Order> getOrders() { return orders; }
再调整下保存顺序
@Test public void testOneToManyPersist() { Customer customer = new Customer(); customer.setEmail("mm@163.com"); customer.setLastName("MM"); Order order1 = new Order(); order1.setOrderName("O-MM-1"); Order order2 = new Order(); order2.setOrderName("O-MM-2"); //建立关联关系 customer.getOrders().add(order1); customer.getOrders().add(order2); order1.setCustomer(customer); order2.setCustomer(customer); //执行保存操作 entityManager.persist(customer); entityManager.persist(order1); entityManager.persist(order2); }
原文:https://www.cnblogs.com/jhxxb/p/10363147.html