欢迎访问 如意编程网!

如意编程网

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

7、JPA-映射-双向一对多

发布时间:2024/7/5 编程问答 3 豆豆
如意编程网 收集整理的这篇文章主要介绍了 7、JPA-映射-双向一对多 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一个用户对应多个订单,多个订单对应一个用户,不管查哪一边都可以得到另一边的信息

 

实体类

Customer

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)@Idpublic 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()public Set<Order> getOrders() {return orders;}public void setOrders(Set<Order> orders) {this.orders = orders;} }

Order

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@Idpublic 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()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;@Beforepublic void init() {entityManagerFactory = Persistence.createEntityManagerFactory("jpaname");entityManager = entityManagerFactory.createEntityManager();transaction = entityManager.getTransaction();transaction.begin();}@Afterpublic 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 属性了 @Testpublic 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

创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖

总结

以上是如意编程网为你收集整理的7、JPA-映射-双向一对多的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得如意编程网网站内容还不错,欢迎将如意编程网推荐给好友。