Spring Data JPA 不保存一对多映射
Spring Data JPA not saving one-to-many mapping
我有 Worker
和 Department
实体。 Department
包含 Worker
的列表,Worker
包含 Department
实体。所以这是一对多的关系。
工人
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Worker {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "worker_sequence")
@SequenceGenerator(name = "worker_sequence", sequenceName = "worker_sequence", allocationSize = 1)
private Long workerId;
private String name;
private String lastName;
private String email;
private LocalDate birth;
@ManyToOne()
@JoinColumn(name = "department_id")
private Department department;
}
部门
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "department_sequence")
@SequenceGenerator(name = "department_sequence", sequenceName = "department_sequence", allocationSize = 1)
private Long departmentId;
private String name;
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
private List<Worker> workers = new ArrayList<>();
public void addWorker(Worker worker) {
workers.add(worker);
}
}
控制器
@RestController
@RequestMapping("api")
public class DepartmentWorkerController {
DepartmentService departmentService;
WorkerService workerService;
@Autowired
public DepartmentWorkerController(DepartmentService departmentService, WorkerService workerService) {
this.departmentService = departmentService;
this.workerService = workerService;
}
@PutMapping("department/{departmentId}/worker/{workerId}")
public Department assignWorkerToDepartment(@PathVariable("departmentId") Long departmentId, @PathVariable("workerId") Long workerId) {
Department department = departmentService.getDepartmentById(departmentId);
Worker worker = workerService.getWorkerByID(workerId);
department.addWorker(worker);
return departmentService.saveDepartment(department);
}
}
问题是没有错误但是关联没有保存。调试时,一切似乎都很好。
查看双向关系在 hibernate.When 中的工作原理您声明了 List 并将关系映射到 Department 不创建另一个 table 并在 worker table 中使用也创建了 department_id =] 作为 relationship.When 的目标,您将 Worker 添加到部门列表 class 在那种情况下,您的 Worker 部门为空,正如您所说的休眠以在您的 Worker 中使用 department_id table 作为关系所有者
hibernate没有意识到这个关系。
要解决您的问题,您需要将部门设置为工作人员 worker.setDepartment(department) 然后将该工作人员添加到部门列表并保存该部门,然后该工作人员将通过级联和 department_id 将设置为您需要单独保存它们
我有 Worker
和 Department
实体。 Department
包含 Worker
的列表,Worker
包含 Department
实体。所以这是一对多的关系。
工人
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Worker {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "worker_sequence")
@SequenceGenerator(name = "worker_sequence", sequenceName = "worker_sequence", allocationSize = 1)
private Long workerId;
private String name;
private String lastName;
private String email;
private LocalDate birth;
@ManyToOne()
@JoinColumn(name = "department_id")
private Department department;
}
部门
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "department_sequence")
@SequenceGenerator(name = "department_sequence", sequenceName = "department_sequence", allocationSize = 1)
private Long departmentId;
private String name;
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL)
private List<Worker> workers = new ArrayList<>();
public void addWorker(Worker worker) {
workers.add(worker);
}
}
控制器
@RestController
@RequestMapping("api")
public class DepartmentWorkerController {
DepartmentService departmentService;
WorkerService workerService;
@Autowired
public DepartmentWorkerController(DepartmentService departmentService, WorkerService workerService) {
this.departmentService = departmentService;
this.workerService = workerService;
}
@PutMapping("department/{departmentId}/worker/{workerId}")
public Department assignWorkerToDepartment(@PathVariable("departmentId") Long departmentId, @PathVariable("workerId") Long workerId) {
Department department = departmentService.getDepartmentById(departmentId);
Worker worker = workerService.getWorkerByID(workerId);
department.addWorker(worker);
return departmentService.saveDepartment(department);
}
}
问题是没有错误但是关联没有保存。调试时,一切似乎都很好。
查看双向关系在 hibernate.When 中的工作原理您声明了 List 并将关系映射到 Department 不创建另一个 table 并在 worker table 中使用也创建了 department_id =] 作为 relationship.When 的目标,您将 Worker 添加到部门列表 class 在那种情况下,您的 Worker 部门为空,正如您所说的休眠以在您的 Worker 中使用 department_id table 作为关系所有者 hibernate没有意识到这个关系。 要解决您的问题,您需要将部门设置为工作人员 worker.setDepartment(department) 然后将该工作人员添加到部门列表并保存该部门,然后该工作人员将通过级联和 department_id 将设置为您需要单独保存它们