JPA笔记4 ManyToMany
阅读原文时间:2023年07月10日阅读:4

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);  
}

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章