2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
《JPA from entry to masteryThe series includes the following articles:
exist Many-to-Many In the association relationship, onlyIntermediate table Mapping can not be achieved by adding foreign keys.
annotation @ManyToMany Used for the sending and receiving ends of a relationship. The sending end of a relationship defines the field attributes of the receiving end of a collection type, and the receiving end of a relationship does not need to be defined.
package com.example.demo.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
@Entity
@Data
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@Column(columnDefinition = "enum('male','female')")
private String sex;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "teacher_student", joinColumns = {@JoinColumn(name = "s_id")}, inverseJoinColumns = {@JoinColumn(name = "t_id")})
private Set<Teacher> teachers;
}
package com.example.demo.entity;
import lombok.Data;
import javax.persistence.*;
import java.util.List;
import java.util.Set;
@Data
@Entity
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY)
/**
* Description:
* 1、关系两边都作为主控;
* 2、joinColumns 中 @JoinColumn(name="t_id") 其中 t_id 为 JoinTable 中的外键,由于 Student 和 Teacher 的主键都为 id 这边就省略 referencedColumnName="id"。
*/
@JoinTable(name = "teacher_student", joinColumns = {@JoinColumn(name = "t_id")}, inverseJoinColumns = {@JoinColumn(name = "s_id")})
private Set<Student> students;
}
exist Many-to-Many Here are some things to keep in mind in a relationship:
joinColumns
of@JoinColumn(name="t_id")
middle,t_id
is the foreign key in JoinTable. Since both Student and Teacher have primary keys ofid
, so it is omitted here referencedColumnName="id"
。cascade = CascadeType.PERSIST
, then in the execution save
Will callonPersist()
This method will recursively call the outer class (Student or Teacher)onPersist()
Cascade addition. But because the value has been added, it will reportdetached entity passed to persist
Error, cancel the cascade operation (removecascade = CascadeType.PERSIST
) is enough.The Service and Repository layers are described inRelational Mapping Development (I): One-to-One Mapping》I have already mentioned that there is no difference here, so I will not repeat it and go directly into the code writing of the test layer.
package com.example.demo.entity;
import com.example.demo.repository.StudentRepository;
import com.example.demo.repository.TeacherRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringRunner.class)
public class ManyToManyTest {
@Autowired
private StudentRepository studentRepository;
@Autowired
private TeacherRepository teacherRepository;
@Test
public void add() {
Set<Teacher> teachers = new HashSet<>();
Set<Student> students = new HashSet<>();
Student student1 = new Student();
student1.setName("张三");
students.add(student1);
studentRepository.save(student1);
Student student2 = new Student();
student2.setName("李四");
students.add(student2);
studentRepository.save(student2);
Teacher teacher1 = new Teacher();
teacher1.setName("皮皮老师");
teacher1.setStudents(students);
teachers.add(teacher1);
teacherRepository.save(teacher1);
}
}
Run the test class,ControllerThe following results are output:
🚀 对于双向 ManyToMany 关系,注解 @ManyToMany 用于关系的发出端和接收端。另外,关系的接收端需要设置
@ManyToMany(mappedBy='集合类型发出端实体的字段名称')
。