单向
学生就正常来
package com.yjj.entity2;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "t_student")
public class Student {
@Id
@GeneratedValue
private Long id;
private String sname;
public Student() {
}
public Student(String sname) {
this.sname = sname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public String toString() {
return "Student [id=" + id + ", sname=" + sname + "]";
}
}
老师里维护一个学生集合
package com.yjj.entity2;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "t_teacher")
public class Teacher {
@Id
@GeneratedValue
private Long id;
private String tname;
// @ManyToMany注释表示Teacher是多对多关系的一端。
// @JoinTable描述了多对多关系的数据表关系。name属性指定中间表名称,joinColumns定义中间表与Teacher表的外键关系。
// 中间表Teacher_Student的Teacher_ID列是Teacher表的主键列对应的外键列,inverseJoinColumns属性定义了中间表与另外一端(Student)的外键关系。
@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
@JoinTable(name = "t_teacher_student", joinColumns = { @JoinColumn(name = "teacher_id") }, inverseJoinColumns = {
@JoinColumn(name = "student_id") })
private Set<Student> students = new HashSet<Student>();
public Teacher() {
}
public Teacher(String tname) {
this.tname = tname;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", tname=" + tname + "]";
}
}
测试类
Teacher t1 = new Teacher("t1");
Teacher t2 = new Teacher("t2");
Student s1 = new Student("s1");
Student s2 = new Student("s2");
Student s3 = new Student("s3");
entityManager.persist(t1);
entityManager.persist(t2);
entityManager.persist(s1);
entityManager.persist(s2);
entityManager.persist(s3);// 全部发出5条insert单表
// 添加中间表
// t1老师教2个学生s1,s2
// t2老师教3个学生s1,s2,s3
t1.getStudents().add(s1);
t1.getStudents().add(s2);
t2.getStudents().add(s1);
t2.getStudents().add(s2);
t2.getStudents().add(s3);
entityManager.persist(s1);
entityManager.persist(s2);
entityManager.persist(s3);
entityManager.persist(t1);
entityManager.persist(t2);
双向
产品和类目中都维护一个对方的集合,注意mappedBy=什么
package com.yjj.entity2;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
/**
* author:殷俊杰
*/
@Table(name="product")
@Entity
public class Product {
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
@Column(name="productName")
private String productName;
@JoinTable(name = "Category_Product",
joinColumns = {@JoinColumn(name = "product_id",referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name="category_id",referencedColumnName = "id")}
)
@ManyToMany
private List<Category> categories=new ArrayList<>();
@Override
public String toString() {
return "Product{" +
"id=" + id +
", productName='" + productName + '\'' +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
package com.yjj.entity2;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Table(name="category")
@Entity
public class Category {
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
@Column(name="categoryName")
private String categoryName;
@ManyToMany(mappedBy = "categories")
private List<Product> products=new ArrayList<>();
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;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
@Override
public String toString() {
return "Category{" +
"id=" + id +
", categoryName='" + categoryName + '\'' +
", products=" + products +
'}';
}
}
测试类
Product product=new Product();
product.setProductName("手机");
Product product1=new Product();
product1.setProductName("电脑");
Category category=new Category();
category.setCategoryName("数码");
Category category1=new Category();
category1.setCategoryName("玩具");
category.getProducts().add(product);
category.getProducts().add(product1);
product.getCategories().add(category);
product.getCategories().add(category1);
entityManager.persist(category);
entityManager.persist(category1);
entityManager.persist(product);
entityManager.persist(product1);
JPA帮我们创建的三张表