1. 程式人生 > >9、JPA-映射-雙向多對多

9、JPA-映射-雙向多對多

cto map before mit fin ima 懶加載 span init

實體類

Category

package com.jpa.yingshe;

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
="JPA_CATEGORIES") @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") public Set<Item> getItems() {
return items; } public void setItems(Set<Item> items) { this.items = items; } }

Item

package com.jpa.yingshe;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

@Table(name = "JPA_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;
    }

    // 使用 @ManyToMany 註解來映射多對多關聯關系
    // 使用 @JoinTable 來映射中間表
    // 1. name 中間表的名字
    // 2. 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 jpa.test;

import com.jpa.yingshe.Category;
import com.jpa.yingshe.Item;
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;

    @Before
    public void init() {
        entityManagerFactory = Persistence.createEntityManagerFactory("jpaname");
        entityManager = entityManagerFactory.createEntityManager();
        transaction = entityManager.getTransaction();
        transaction.begin();
    }

    @After
    public void destroy() {
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }
    
    @Test
    public void testManyToManyPersist() {
        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);
    }
}

技術分享圖片

技術分享圖片

查詢

// 對於關聯的集合對象, 默認使用懶加載的策略
// 使用維護關聯關系的一方獲取, 還是使用不維護關聯關系的一方獲取, SQL 語句相同
@Test
public void testManyToManyFind(){
    Item item = entityManager.find(Item.class, 36);
    System.out.println(item.getItemName());

    System.out.println(item.getCategories().size());
}

技術分享圖片

通過另一邊查詢

@Test
public void testManyToManyFind(){
    Category category = entityManager.find(Category.class, 38);
    System.out.println(category.getCategoryName());
    System.out.println(category.getItems().size());
}

技術分享圖片

9、JPA-映射-雙向多對多