entity Item
package entity; import java.util.HashSet; import java.util.Set; 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 = "tb_items") @Entity public class Item { private Integer id; private String itemName; /** * 多对多的关系 */ private Set<Category> categories = new HashSet<>(); @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "ITEM_NAME") public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } // joinColumns:指定自己的外键和中间表之关联的之列 // @JoinColumn(name="ITEM_ID" 中间表的外键 // referencedColumnName="ID" 自己的主键 // ****inverseJoinColumns:指定与之相关表的外键和与之关联的之列 //name="ITEM_CATEGORY":指定中间表的表名 @JoinTable(name="ITEM_CATEGORY", joinColumns = { @JoinColumn(name = "ITEM_ID", referencedColumnName = "ID") }, inverseJoinColumns = {@JoinColumn(name = "CATEGORY_ID", referencedColumnName = "ID") }) @ManyToMany public Set<Category> getCategories() { return categories; } public void setCategories(Set<Category> categories) { this.categories = categories; } @Override public String toString() { return "Item [id=" + id + ", itemName=" + itemName + ", categories=" + categories + "]"; } }
entity Category
package entity; import java.util.HashSet; import java.util.Set; 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="TB_CATEGORY") @Entity public class Category { private Integer id; private String categoryName; private Set<Item> items = new HashSet<>(); @GeneratedValue @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="CATEGORY_NAME") public String getCategoryName() { return categoryName; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } //多对多的关系 @ManyToMany(mappedBy="categories") //放弃维护关系,有item维护 public Set<Item> getItems() { return items; } public void setItems(Set<Item> items) { this.items = items; } @Override public String toString() { return "Category [id=" + id + ", categoryName=" + categoryName + ", items=" + items + "]"; } }
testManyToMany
package test; import java.util.Set; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import org.junit.After; import org.junit.Before; import org.junit.Test; import entity.Category; import entity.Item; public class ManyToManyTest { private EntityManagerFactory entityManagerFactory; private EntityManager entityManager; private EntityTransaction entityTransaction; @Before public void init() { entityManagerFactory = Persistence.createEntityManagerFactory("JPA"); entityManager = entityManagerFactory.createEntityManager(); entityTransaction = entityManager.getTransaction(); entityTransaction.begin(); } @After public void distory() { entityTransaction.commit(); entityManager.close(); entityManagerFactory.close(); } @Test public void testFind(){ //使用关系不维护的一方获取, Category category = entityManager.find(Category.class, 27); Set<Item> items = category.getItems(); for (Item item : items) { System.out.println(item.getItemName()); } // //对于关联的对象默认使用懒加载 ,使用关系维护的 一方获取还是使用关系不维护的一方获取,sql语句相同 // Item item = entityManager.find(Item.class, 25); // Set<Category> categories = item.getCategories(); // for (Category category : categories) { // System.out.println(category.getCategoryName()); // } } @Test public void testmanyToManyPersistence() { Category category1 = new Category(); category1.setCategoryName("c1"); Category category2 = new Category(); category2.setCategoryName("c2"); Item item1 = new Item(); item1.setItemName("i1"); Item item2 = new Item(); item1.setItemName("i2"); //给予关系 category1.getItems().add(item1); category1.getItems().add(item2); category2.getItems().add(item1); category2.getItems().add(item2); item1.getCategories().add(category1); item1.getCategories().add(category2); item2.getCategories().add(category1); item2.getCategories().add(category2); //持久化 entityManager.persist(item1); entityManager.persist(item2); entityManager.persist(category1); entityManager.persist(category2); } }