【发布时间】:2018-10-15 11:20:14
【问题描述】:
假设我们有以下三个域模型实体:Company、Departament 和 Employee。
@Getter @Setter @NoArgsConstrutor
public class Employee {
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "department_id", nullable = false, insertable = false, updatable = false)
private Department department;
@JoinColumn(name = "department_id", nullable = false)
private int department_id;
}
@Getter @Setter @NoArgsConstrutor
public class Department {
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "company_id", nullable = false, insertable = false, updatable = false)
private Company company;
@JoinColumn(name = "company_id", nullable = false)
private int company_id;
@OneToMany(mappedBy = "department")
private List<Employee> employees;
}
@Getter @Setter @NoArgsConstrutor
private class Company {
private String name;
@OneToMany(mappedBy = "company")
private List<Department> departments;
}
对于每个实体,我们都有 Repositories 扩展 JpaRepository、Services 和 Controllers。在每个Service 中我们@Autowire 各自的Repository,在每个实体Controller 中我们调用来自实体Service 的方法。
我的问题如下:我无法保存整个Company,因为Departments 需要Company ID,而Employees 需要Deparment ID。因此,首先,在我的CompanyService 中保存然后清除departments 列表,执行saveAndFlush 为我的company 分配一个ID。我将接收到的 ID 分配给先前保存的 departments 列表的每个实体中的每个 company_id,然后将该列表附加到 company 并执行另一个 saveAndFlush,我再为 @987654347 执行一次@列表。
@RestController
public class CompanyController {
@Autowire
private CompanyService companyService;
@PostMapping("/companies")
public Company createCompany(@RequestBody Company newCompany) {
return companyService.createCompany(newCompany);
}
}
@Service
public class CompanyService {
@Autowire
private CompanyRepository companyRepository;
public Company createCompany(Company company) {
List<Department> departments = new ArrayList<>(company.getDepartments());
company.getDepartments().clear();
companyRepository.saveAndFlush(company);
int company_id = company.getId();
departments.forEach (department ->
department.setCompany_id(company_id);
);
//here I save a copy of the previously saved departments, because I still need the employees
company.getDepartments().addAll(departments.stream().map(department -> department.clone(department)).collect(Collectors.toList()));
company.getDepartments().forEach(department -> department.getEmployees().clear());
companyRepository.saveAndFlush(company);
//here I assign each employee it's corresponding department ID
for (int i = 0; i < company.getDepartments().size(); i++) {
Department departmentInSavedCompany = company.getDepartments().get(i);
Department departmentWhichStillHasEmployees = departments.get(i);
departmentWhichStillHasEmployees.setId(departmentInSavedCompany.getId());
departmentWhichStillHasEmployees.getEmployees().forEach(employee -> employee.setDepartment_id(departmentInSavedCompany.getId()));
}
company.getDepartments.clear();
company.getDepartments.addAll(departments);
return companyRepository.saveAndFlush(company);
}
}
@Repository
public interface CompanyRepository extends JpaRepository<Company, Integer> {
}
我目前不喜欢这个实现,也不觉得它很好。哪种方法适合这种情况?
【问题讨论】:
-
为什么将引用的 id 作为实体中的单独属性?
-
因为在发布新员工时,我只需传递部门 id 即可建立关系。
POST { "name" : "New Employee", "department_id" : 1}
标签: java spring-mvc model-view-controller spring-data-jpa