这是控制器类中的控制器方法
@ApiOperation(value="Create a new employee")
@ApiResponses({})
@RequestMapping(value= "/createEmployee",method= RequestMethod.POST)
public Callable<EmployeeDTO> createEmployee(
HttpServletRequest request,
@RequestBody @Valid final EmployeeDTO employeeDto) {
long employeeId = employeeService.createEmployee(employeeDto);
EmployeeDTO createdEmployee = employeeService.getEmployee(employeeId);
return () -> createdEmployee;
}
这是服务类
中createEmployee(employeeDto)的方法 @Transactional
public long createEmployee(employeeDto){
Employee employee = new Employee();
employee.setName(employeeDto.getName());
employee.setCompanyId(employeeDto.getCompanyId());
Employee savedEmployee = employeeRepository.save(employee);
return savedEmployee.getEmployeeId();
}
这是与上面相同的服务类中的getEmployee方法。
public EmployeeDTO getEmployee(long employeeId) {
Employee employee = employeeRepository.findByEmployeeId(employeeId);
EmployeeDTO employeeDTO = new EmployeeDTO();
employeeDTO.setEmployeeId(employee.getEmployeeId());
employeeDTO.setName(employee.getName());
employeeDTO.setCompanyByCompanyId(employee.getCompanyByCompanyId());
}
在上面的代码中,只有在控制器方法完成执行后,才会持久保存employee对象吗?
在调试期间,我看到它正在插入数据库中。但是,在访问引用的实体对象时,employee.getCompanyByCompanyId()
显示为NULL。所以我不确定它是否因为持久性没有正确发生或者是其他原因。请帮忙。
以下是实体类
@Entity
@Table(name = "employees")
public class Employee {
private long employeeId;
private String name;
private long companyId;
private Company companyByCompanyId;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "employee_id")
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "company_id")
public long getCompanyId() {
return companyId;
}
public void setCompanyId(long companyId) {
this.companyId = companyId;
}
@ManyToOne
@JoinColumn(name = "company_id", referencedColumnName = "company_id", nullable = false, insertable = false, updatable = false)
public Company getCompanyByCompanyId() {
return companyByCompanyId;
}
public void setCompanyByCompanyId(Company companyByCompanyId) {
this.companyByCompanyId = companyByCompanyId;
}
}
和公司实体类如下
@Entity
@Table(name = "companies")
public class Company {
private long companyId;
private String companyName;
private Collection<Employee> employeesByCompanyId;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "company_id")
public long getCompanyId() {
return companyId;
}
public void setCompanyId(long companyId) {
this.companyId;
}
@Basic
@Column(name = "company_name")
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
@OneToMany(mappedBy = "companyByCompanyId")
public Collection<Employee> getEmployeesByCompanyId() {
return employeesByCompanyId;
}
public void setEmployeesByCompanyId(Collection<Employee> employeesByCompanyId) {
this.employeesByCompanyId = employeesByCompanyId;
}
}
public interface ExmployeeRepository extends JpaRepository<Employee,Long>, JpaSpecificationExecutor<Employee> {
Employee findByEmployeeId(long employeeId);
}