新增SchoolDTO
package com.example.demo1025.dto;
import java.io.Serializable;
import java.util.List;import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;@Entity
@Table(name = "school")
public class SchoolDTO implements Serializable {private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private int id;private String schoolName;
// 一个学校对应多个学生,用list
// CascadeType.PERSIST 设置 :级联 保存/新建 操作 。新建 学校和学生 的时候,保存新建的学校那么新建的学生也同时被保存
// CascadeType.REMOVE 级联删除,学校删除的时候学生也会被删除
@OneToMany(mappedBy = "school", cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
private List<StudentDTO> studentList;public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getSchoolName() {
return schoolName;
}public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}public List<StudentDTO> getStudentList() {
return studentList;
}public void setStudentList(List<StudentDTO> studentList) {
this.studentList = studentList;
}}
在 StudentDTO新增school
package com.example.demo1025.dto;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;@Entity
@Table(name = "student")
public class StudentDTO implements Serializable {private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
@Column(name = "student_id")
private int id;private String studentName;
// 设置cascade,否则会报错object references an unsaved transient instance - save the
// transient instance before flushing :
// com.example.demo1025.dto.StudentDTO.score ->
// com.example.demo1025.dto.ScoreDTO
@OneToOne(cascade = CascadeType.ALL)
private ScoreDTO score;// 多个学生对应一个学校
@ManyToOne
private SchoolDTO school;public int getId() {
return id;
}public void setId(int id) {
this.id = id;
}public String getStudentName() {
return studentName;
}public void setStudentName(String studentName) {
this.studentName = studentName;
}public ScoreDTO getScore() {
return score;
}public void setScore(ScoreDTO score) {
this.score = score;
}public SchoolDTO getSchool() {
return school;
}public void setSchool(SchoolDTO school) {
this.school = school;
}}
新增StudentSchoolRepository
package com.example.demo1025.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo1025.dto.SchoolDTO;
public interface StudentSchoolRepository extends JpaRepository<SchoolDTO, Integer> {
}
StudentScoreService 新增addSchoolStudent添加方法
@Autowired
private StudentSchoolRepository studentSchoolRepository;/**
* 级联添加
*/public void addSchoolStudent() {
SchoolDTO schoolDTO = new SchoolDTO();
schoolDTO.setSchoolName("春田花花幼儿园");StudentDTO studentDTO = new StudentDTO();
studentDTO.setStudentName("小花");
studentDTO.setSchool(schoolDTO);StudentDTO studentDTO1 = new StudentDTO();
studentDTO1.setStudentName("小花1");
studentDTO1.setSchool(schoolDTO);List<StudentDTO> studentList = new ArrayList<>();
studentList.add(studentDTO);
studentList.add(studentDTO1);schoolDTO.setStudentList(studentList);
studentSchoolRepository.save(schoolDTO);}
/**
* 级联删除
*/
public void deleteSchool() {
studentSchoolRepository.deleteById(21);
}
StuentScoreController 新增addStudentSchool方法
@RequestMapping("addStudentSchool")
@ResponseBody
public String addStudentSchool() {
studentScoreService.addSchoolStudent();
return "添加成功";
}
@RequestMapping("deleteStudentSchool")
@ResponseBody
public String deleteStudentSchool() {
studentScoreService.deleteSchool();
return "删除成功";
}