package many_to_many; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; @Table(name = "JPA_CATEGORY") @Entity public class Category { private Integer id; private String categoryName; private Set<Item> items = new HashSet<>(); @GeneratedValue(strategy = GenerationType.AUTO) @Id public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getCategoryName() { return categoryName; } public void setCategoryName(String categoryName) { this.categoryName = categoryName; } @ManyToMany(mappedBy = "categories") public Set<Item> getItems() { return items; } public void setItems(Set<Item> items) { this.items = items; } }
package many_to_many; import java.util.HashSet; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; @Table(name = "JPA_ITEM") @Entity public class Item { private Integer id; private String itemName; private Set<Category> categories = new HashSet<>(); @GeneratedValue(strategy = GenerationType.AUTO) @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; } /* * 使用@ManyToMany註解來映射多對多關聯關係 * 使用@JoinTable來映射中間表 * 1.name指向中間表的名字 * 2.j joinColumns映射當前類所在的表在中間表中的外鍵 * 2.1 name指定外鍵列的列名 * 2.2 referencedColumnName指定外鍵列關聯當前表的哪一列 * 3. inverseJoinColumns映射關聯的類所在中間表的外鍵 * */ @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; } }
package many_to_many; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; public class Main4 { public static void main(String[] args) { // TODO Auto-generated method stub String persistenceUnitName = "jpa-1"; EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName); EntityManager entityManager = entityManagerFactory.createEntityManager(); EntityTransaction transaction = entityManager.getTransaction(); transaction.begin(); insert(entityManager); transaction.commit(); entityManager.close(); entityManagerFactory.close(); } // 1.多對多的保存 private static void insert(EntityManager entityManager) { Item i1 = new Item(); i1.setItemName("i-1"); Item i2 = new Item(); i2.setItemName("i-2"); Category c1 = new Category(); c1.setCategoryName("c-1"); Category c2 = new Category(); c2.setCategoryName("c-2"); i1.getCategories().add(c1); i1.getCategories().add(c2); i2.getCategories().add(c1); i2.getCategories().add(c2); c1.getItems().add(i1); c1.getItems().add(i2); c2.getItems().add(i1); c2.getItems().add(i2); entityManager.persist(i1); entityManager.persist(i2); entityManager.persist(c1); entityManager.persist(c2); } }