有一个名为“Person”的实体和一个名为“Course”的实体,具有许多关系。连接实体被称为“PersonCourse”。Spring和Hibernate:用于包含对象的对象的形式
@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为空。什么可能是错的?
你已经确定数据已经保存在你的数据库中吗?并用你的方法,你可以得到人道课程? – Angga
是的,我有三个表中的数据,我可以毫无问题地检索这些数据。唯一的问题是,当我尝试使用person.id保存人行道 – user1883212