有一个名为“Person”的实体和一个名为“Course”的实体,具有多对多的关系。连接实体称为“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
}
我有以下表格,我应该将信息保存到“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>
在控制器中,我尝试使用以下方法读取信息:
@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
}
但由于某种原因 personcourse.person 和 personcourse.course 为空。可能有什么问题?