Spring and Hibernate: form for object containing objects
forms, hibernate, java, spring
Solution
Try to change to:
<form:select path="person.id">
<form:select path="course.id">
or try to use `Formatters`.
Problem
There are an entity called "Person" and an entity called "Course" with relation many to many. The join entity is called "PersonCourse". ``` @Entity @Table(name="person_course") public class PersonCourse implements Serializable { @Id @ManyToOne @JoinColumn(name="course_ID") private Course course; @Id @ManyToOne @JoinColumn(name="person_ID") private Person person; // Getter and setters here } ``` I've the following form with which I'm supposed to persist information to "PersonCourse": ``` <form:form method="post" action="addpersoncourse.html" commandName="personcourse"> <form:label path="person"><spring:message code="label.person"/></form:label> <form:select path="person"> <c:forEach items="${personList}" var="person"> <form:option value="${person.id}">${person.firstName} ${person.lastName}</form:option> </c:forEach> </form:select> <form:label path="course"><spring:message code="label.course"/></form:label> <form:select path="course"> <c:forEach items="${courseList}" var="course"> <form:option value="${course.id}">${course.course}</form:option> </c:forEach> </form:select> <input type="submit" value="<spring:message code="label.addpersoncourse"/>"/> </form:form> ``` In the controller I'm trying to read the information with the following method: ``` @RequestMapping(value = "/addpersoncourse", method = RequestMethod.POST) public String addpersoncourse(@ModelAttribute("personcourse") PersonCourse personCourse, BindingResult result) { // HERE personcourse.person and personcourse.course ARE NULL // Other operation I'm supposed to do } ``` But for some reason personcourse.person and personcourse.course are null. What may be wrong?