仔细查看您的
Employee课程:
@Entitypublic class Employee{ ... @ManyToOne @JoinColumn(name = "department_id") private Department department; //Setter public void setDepartment(Department department) { this.department = department } ... // Getters and Setters}为了设置一个
department创建实例
Department,然后通过setter发送它:
@RequestMapping(value = "/saveEmployee", method = RequestMethod.POST)public String saveEmployee(@ModelAttribute("employee") Employee employee){ int id = 1; // 2 or 3... Department temporaryDepartment = new Department(); temporaryDepartment.setId(id); employee.setDepartment(temporaryDepartment); employeeService.saveEmployee(employee); return "redirect:/employees";}


