Spring and Hibernate: form of an object containing objects

There is an object called "Man" and an object called "Course" with the attitude of many to many. The union object 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 have the following form with which I must save information in "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 try to read the information in the following way:

 @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 could be wrong?

+4
source share
1 answer

Try changing to:

 <form:select path="person.id"> <form:select path="course.id"> 

or try using Formatters .

+4
source

Source: https://habr.com/ru/post/1490604/


All Articles