JPA學習筆記(10)——對映雙向多對多關聯關係
阿新 • • 發佈:2018-12-21
多對多關聯
比如現在有兩個實體類:1. Product(商品)2. Category(類別)
一個商品可以有多個類別,一個類別也可以有多個商品,這就形成了多對多的關係
Product
package com.jpa.helloworld2;import java.util.List;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.Table;@Table(name="T_PRODUCT")@Entitypublic class Product { @GeneratedValue @Id @Column(name="ID") private Integer id; @Column(name="NAME") private String name; @JoinTable(name="PRODUCT_CATEGORY" ,//中間表的名稱 joinColumns={@JoinColumn(name="PRODUCT_ID",referencedColumnName="ID")},//中間表PRODUCT_ID欄位關聯PRODUCT的ID inverseJoinColumns={@JoinColumn(name="CATEGORY_ID",referencedColumnName="ID")})//中間表CATEGORY_ID欄位關聯CATEGORY的ID @ManyToMany private List<Category> categorys = new ArrayList<Category>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Category> getCategorys() { return categorys; } public void setCategorys(List<Category> categorys) { this.categorys = categorys; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
Category
package com.jpa.helloworld2;import java.util.List;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.Table;@Table(name="T_CATEGORY")@Entitypublic class Category { @GeneratedValue @Id @Column(name="ID") private Integer id; @Column(name="NAME") private String name; @ManyToMany(mappedBy="categorys") private List<Product> products = new ArrayList<Product>(); public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Product> getProducts() { return products; } public void setProducts(List<Product> products) { this.products = products; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
多對多關係需要用到第三張表,不過這張表不需要我們手動建立,JPA會根據我們使用註解配置的來幫我們建立,我們只需要寫好上面的兩個實體類即可。
JPA建立的三張表:
中間表中的外來鍵關係
增加
Category category1 = new Category();category1.setName("category1");Category category2 = new Category();category2.setName("category2");Product product1 = new Product();product1.setName("product1");Product product2 = new Product();product2.setName("product2");category1.getProducts().add(product1);category1.getProducts().add(product2);category2.getProducts().add(product1);category2.getProducts().add(product2);product1.getCategorys().add(category1);product1.getCategorys().add(category2);product2.getCategorys().add(category1);product2.getCategorys().add(category2);entityManager.persist(category1);entityManager.persist(category2);entityManager.persist(product1);entityManager.persist(product2);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
查詢
對於關聯的集合物件,預設使用懶載入的策略
無論查詢維護關聯關係的一方,還是查詢不維護關聯關係的一方,SQL語句相同。