更新父对象时删除特定子对象的Spring Boot JPA技术问题
各位大佬,我现在基于Spring Boot和Spring JpaRepository做开发,部门实体Department关联了员工列表Employee,创建部门的功能没问题,但更新部门的时候遇到两个棘手的问题,想请教下大家:
实体类定义
Department实体
@Entity @Table(name="department") public class Department{ @Id @Column(name="dept_id") @GeneratedValue(strategy=GenerationType.AUTO) public Integer id; @Column(name="dept_name") public String name; @Column(name="dept_code") public String code; @OneToMany(cascade = {CascadeType.REMOVE,CascadeType.REFRESH},orphanRemoval = true, fetch = FetchType.LAZY,mappedBy="department") public List<Employee> employees; }
Employee实体
@Entity @Table(name="employee") public class Employee { @Id @Column(name="emp_id") @GeneratedValue(strategy=GenerationType.AUTO) public Integer id; @Column(name="emp_name") public String name; @ManyToOne @JoinColumn(name = "dept_id") public Department department; @Transient public String operation; }
服务层代码
@Transactional @Service public class DepartmentService { public Department createDepartment(Department department) { Department dept = departmentRepository.save(department); for (Employee emp : department.getEmployees()) { emp.setDepartment(department); employeeRepository.save(emp); } return dept; } public Department updateDepartment(Department department) { Department dept = departmentRepository.save(department); if (!dept.getEmployees().isEmpty()) { for (Employee emp : department.getEmployees()) { if (emp.getOperation().equalsIgnoreCase("delete")) employeeRepository.deleteById(emp.getId()); else employeeRepository.save(emp); } } return dept; } public Department getDepartment(int id) { return departmentRepository.getOne(id); } }
Controller代码
@RestController public class DepartmentController { @Autowired DepartmentService departmentService; private ModelMapper modelMapper = new ModelMapper(); @RequestMapping(value = "/dept", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<DepartmentBean> updateDepartment(@RequestBody DepartmentBean deptBean) { Department dept = modelMapper.map(deptBean, Department.class); Department persistedDept = departmentService.updateDepartment(dept); Department d = departmentService.getDepartment(persistedDept.getId()); DepartmentBean userDTO = modelMapper.map(d, DepartmentBean.class); return new ResponseEntity<DepartmentBean>(userDTO, HttpStatus.OK); } @RequestMapping(value = "/dept/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public ResponseEntity<DepartmentBean> getDepartment(@PathVariable int id) { Department dept = departmentService.getDepartment(id); DepartmentBean userDTO = modelMapper.map(dept, DepartmentBean.class); return new ResponseEntity<DepartmentBean>(userDTO, HttpStatus.OK); } }
更新接口使用的JSON示例
{ "id": 5, "name": "DEPT061", "code": "CODE061", "employees": [ ] }
遇到的具体问题
- 服务层标注了
@Transactional,查询接口返回的是Department代理对象,在服务层内部没法获取员工列表,但用ModelMapper转换成DepartmentBean的时候却能拿到——为啥事务外能从代理对象获取员工列表? - 更新方法返回的代理对象在服务层内部没法获取员工列表,更新后Controller调用查询方法也同样拿不到员工列表。
内容的提问来源于stack exchange,提问作者shivam
相关产品推荐
相关产品推荐

