Spring boot SpringBoot:如何只与给定id的实体建立多对多关系?
我有两个表(考试和部门)和一些填充数据,它们之间存在多对多关系Spring boot SpringBoot:如何只与给定id的实体建立多对多关系?,spring-boot,Spring Boot,我有两个表(考试和部门)和一些填充数据,它们之间存在多对多关系 @Entity public class Exam { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank @Size(min = 5) private String name; @ManyToMany(fetch = FetchType.LAZY) @
@Entity
public class Exam {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(min = 5)
private String name;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "exam_department",
joinColumns = @JoinColumn(name = "exam_id"),
inverseJoinColumns = @JoinColumn(name = "department_id"))
@JsonIgnoreProperties("exams")
private Set<Department> departments = new HashSet<>();
// getters, setters
@Entity
@JsonInclude(Include.NON_NULL)
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToMany(mappedBy = "departments", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnoreProperties("departments")
private List<Exam> exams;
// getters, setters
那个么,这就是我如何发送id的表单给邮递员并导出它们之间的关系的方式吗
我应该做些什么来消除只具有给定id的持久数据之间的关系
@PostMapping("/api/exam")
public ResponseEntity<?> addExamDepartment(@RequestBody Exam exam) {
Exam ex = examRepository.save(exam);
if (ex != null) {
return ResponseEntity.ok(new ApiResponse(true, "Exam Department relation added.", ex));
}
return new ResponseEntity<>(new ApiResponse(false, "Exam Department relation not added.", null),
HttpStatus.BAD_REQUEST);
}
{
"id": 1,
"departments" : [
{
"id" : 1
}, {
"id" : 2
}]
}