我是靠谱客的博主 爱听歌棒棒糖,这篇文章主要介绍JPA基础(五)之jpa里用注解进行映射,现在分享给大家,希望可以做个参考。

jpa 里用注解进行关系映射

单向多对一关联

在 pojo 包下添加实体类 CustomerType.java
在 Customer 类中移除 typeId 属性,添加 CustomerType 属性

复制代码
1
2
3
4
@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name="typeId" ) private CustomerType type;

测试用例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
@Test public void testManyToOne() { // EntitymanagerFactory EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpademo"); // MyBatis---SqlSession JPA --- EntityManager EntityManager entityManager = factory.createEntityManager(); Customer customer= entityManager.find(Customer.class,1); System.out.println(customer); }

注意:注解@ManyToOne(fetch=FetchType.EAGER)默认的是立即加载,需要懒加载时候,要加 fetch 参数修改为 LAZY;可以使用 targetEntity 属性指定关联实体类型@ManyToOne(targetEntity=CustomerType.class)

单向一对多的关联

在 CustomerType.java 中添加 Customer 类型的集合属性

复制代码
1
2
3
4
@OneToMany(targetEntity=Customer.class ,cascade = CascadeType.REMOVE) @JoinColumn(name=”typeid”) private Set<Customer> customers = new HashSet<>();

测试用例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test public void testOneToMany() { // EntitymanagerFactory EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpademo"); // MyBatis---SqlSession JPA --- EntityManager EntityManager entityManager = factory.createEntityManager(); CustomerType customertype= entityManager.find(CustomerType.class,1); System.out.println(customertype.getId()+"==="+customertype.getTypeName()); System.out.println("================================"); Set<Customer> customers= customertype.getCustomers(); Iterator iterator= customers.iterator(); while(iterator.hasNext()){ Customer customer= (Customer) iterator.next(); System.out.println(customer.getId()+"==="+customer.getName()); } }

注意:注解@OneToMany(fetch=FetchType.LAZY)默认的是懒加载,需要懒加载的候,要加 fetch 参数修改,使用@JoinColumn(name=“typeid”) 设置关联外键

双向的一对多的关联

CustomerType.java 中

复制代码
1
2
3
@OneToMany(mappedBy = "type", fetch = FetchType.EAGER ,cascade = CascadeType.REMOVE) private Set<Customer> customers = new HashSet<>();

CustomerType.java 中

复制代码
1
2
3
4
@ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name="typeId" ) private CustomerType type;

注意:

  1. 在 Customer.java 中使用 mappedBy 设置关联的外键属性,避免出现中间表
  2. 双向关联关系,两遍默认都会维护关系,可能冲突,也影响效率,正常情况关系交给“多”的一端维护,一的这端@OneToMany(mappedBy=“type”,这个属性放弃关系维护
  3. 一旦使用 mappedBy 属性,那么@JoinColumn 这个注解就不能使用了,用了会出错

测试用例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Test public void testManyToMany() { // EntitymanagerFactory EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpademo"); // MyBatis---SqlSession JPA --- EntityManager EntityManager entityManager = factory.createEntityManager(); CustomerType customertype= entityManager.find(CustomerType.class,1); System.out.println(customertype.getId()+"==="+customertype.getTypeName()); System.out.println("================================"); Set<Customer> customers= customertype.getCustomers(); Iterator iterator= customers.iterator(); while(iterator.hasNext()){ Customer customer= (Customer) iterator.next(); System.out.println(customer.getId()+"==="+customer.getName()); } }
向多对多的关联

在 pojo 包下添加实体类客户产品实体类 CustomerProduct.java 和产品实体类
Product.java 并加上 JPA 注解
Customer.java 类中添加 manytomany 注解

复制代码
1
2
3
4
5
6
7
@ManyToMany @JoinTable(name = "customer_product", joinColumns = {@JoinColumn(name = "customer_id", referencedColumnName = "id")}, inverseJoinColumns = {@JoinColumn(name = "product_id", referencedColumnName = "id")}) private Set<Product> productItems = new HashSet<Product>();

解释: @JoinTable(name=“中间表”,joinColumns={@JoinColumn(name=“当前对象在中间表的外键名”,referencedColumnName="
当前对象表的主键")},
inverseJoinColumns={@JoinColumn(name="关联表在中间表的外键名
",referencedColumnName=“关联表的主键”)})
Customer 这边维护关系,Product 就要放弃关系的维护,使用 mappedBy 将维护权交给另外
一方。

Product.java 类中

复制代码
1
2
3
@ManyToMany(mappedBy = "productItems") private Set<Customer> customerItem=new HashSet<Customer>();

注意:在真项目开发中,我们处理双向多对多的关联,还可以变通来处理,就是将多对多关联,转换为两个一对多的关联

其他相关JPA知识及代码点此处

最后

以上就是爱听歌棒棒糖最近收集整理的关于JPA基础(五)之jpa里用注解进行映射的全部内容,更多相关JPA基础(五)之jpa里用注解进行映射内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(57)

评论列表共有 0 条评论

立即
投稿
返回
顶部