There is a simple bunch of objects in the database: department and employee (1 to n)

Models:

@Entity public class Department implements Serializable { private static final long serialVersionUID = -3532377236419382984L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int departmentId; @NotEmpty(message = "The department name must not be null") private String departmentName; @NotEmpty(message = "The department description must not be null") private String departmentDescription; @OneToMany(mappedBy = "department", cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JsonIgnore private List<Employee> employeeList; public int getDepartmentId() { return departmentId; } public void setDepartmentId(int departmentId) { this.departmentId = departmentId; } public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } public String getDepartmentDescription() { return departmentDescription; } public void setDepartmentDescription(String departmentDescription) { this.departmentDescription = departmentDescription; } public List<Employee> getEmployeeList() { return employeeList; } public void setEmployeeList(List<Employee> employeeList) { this.employeeList = employeeList; } } @Entity public class Employee implements Serializable { private static final long serialVersionUID = -3532377236419382983L; @Id @GeneratedValue(strategy = GenerationType.AUTO) private int employeeId; @NotEmpty (message = "The employee name must not be null") private String employeeName; @Length(min = 7, max = 12, message = "The employee phone must mot be less then 7 numbers and more then 12 numbers") private String employeePhone; @NotEmpty (message = "The employee role must not be null") private String employeeRole; @NotNull(message = "The employee department must not be null") @ManyToOne @JoinColumn(name = "departmentId") private Department department; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this.employeeId = employeeId; } public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public String getEmployeePhone() { return employeePhone; } public void setEmployeePhone(String employeePhone) { this.employeePhone = employeePhone; } public String getEmployeeRole() { return employeeRole; } public void setEmployeeRole(String employeeRole) { this.employeeRole = employeeRole; } public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } } 

There is a controller that adds new employees:

 @RequestMapping("/admin/employeeBase/addEmployee") public String addEmployee(Model model){ Employee employee = new Employee(); List<Department> departmentList = departmentDao.getAllDepartments(); Department department = new Department(); employee.setDepartment(department); model.addAttribute("employee", employee); model.addAttribute("departmentList", departmentList); return "addEmployee"; } @RequestMapping(value = "/admin/employeeBase/addEmployee", method = RequestMethod.POST) public String addEmployeePost(@Valid @ModelAttribute("employee") Employee employee, BindingResult result, Model model, HttpServletRequest request){ if(result.hasErrors()){ List<Department> departmentList = departmentDao.getAllDepartments(); model.addAttribute("departmentList", departmentList); return "addEmployee"; } employeeDao.addEmployee(employee); return "redirect:/admin/employeeBase"; } 

And the form itself:

  <form:form action="${pageContext.request.contextPath}/admin/employeeBase/addEmployee" method="post" commandName="employee" > <div class="form-group"> <label for="name">Name</label> <form:input path="employeeName" id="name" class="form-Control" /> <form:errors path="employeeName" cssStyle="color:#ff0000;" /> </div> <div class="form-group"> <label for="department">Department: </label> <c:forEach var="department" items="${departmentList}"> <form:radiobutton path="department" id="department" value="department" label="${department.departmentName}" required="required"/> <form:errors path="department" cssStyle="color:#ff0000;" /> </c:forEach> </div> <div class="form-group"> <label for="phone">Phone</label> <form:input path="employeePhone" id="phone" class="form-Control" /> <form:errors path="employeePhone" cssStyle="color:#ff0000;" /> </div> <div class="form-group"> <label for="employeeRole">Role</label> <form:input path="employeeRole" id="role" class="form-Control" /> <form:errors path="employeeRole" cssStyle="color:#ff0000;" /> </div> <br/><br/> <input type="submit" value="submit" class="btn btn-default"> <a href="<c:url value="/admin/employeeBase" />" class="btn btn-default">Cancel</a> </form:form> 

The point is: when the administrator wants to add a new employee. he sees the form of ordinary fields and a list of radiobats (departments). Department we have a separate entity.

Question: how to transfer the department along with the usual employee data?

Now the error takes off:

Java.lang.String to required type com.taskmanager.model.Department for property department; jested.lang.IllegalStateException: Can't convert the type of java.lang.String] to the required type of com. taskmanager.model.Department

    2 answers 2

    add the Long departamentId field to the Employee class with the corresponding column in the database. From the corresponding REFERENCES to the department ID. When registering an employee, the department ID is indicated, or the department name is immediately displayed in the form.

    • It turns out that we have to store the department object itself (in which there is an id) and also create a separate field for this id? Is this exactly the right decision? Unfortunately, nowhere can I find an example of such a request with custom fields (with simple fields) - Vladimir Vladimirovich
    • you have an entity getter public Department getDepartment () {return department; }, take from it. See examples on the One-to-Many relationship - naut92

    The problem was in the mapping of radiobuttons on the department

    Decision:

     <form:radiobutton path="department.departmentId" id="department" value="${department.departmentId}" label="${department.departmentName}" required="required"/>