我正在使用Hibernate和Spring MVC。我有以下形式ManyToOne - 保存時未設置外鍵
當我提交我想我的數據庫表來填補像下面的形式:
表名:學生
student_id studentName
1. Jason Stathum
表名:studentdetails
studentDetailsid FatherName MotherName city_id student_id
1 Mr.X Mrs. Y 1 1
但是當我真正運行我的應用程序並插入表單數據時,除非列city_id
沒有被填滿,否則一切正常。該表看起來如下:
表名:studentdetails
studentDetailsid FatherName MotherName city_id student_id
1 Mr.X Mrs. Y NULL 1
僅供參考,我也有一臺名爲城市,它看起來像下面
表名:studentdetails
city_ID city_name
1 NewYork
我已在下面列入我的代碼,請告訴我h要解決這個問題嗎?
非常感謝您
實體:學生
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="student_id", nullable= false)
private Integer studentId;
private String studentName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name="student_id", referencedColumnName="student_id")
private List<StudentDetails> studentDetails = new ArrayList<StudentDetails>();
// Getters and Setters
實體:StudentDetails
@Entity
@Table(name = "studentDetails")
public class StudentDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer studentDetailsId;
private String FatherName;
private String MotherName;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="city_id")
private City city;
// Getters and Setters
實體:市
@Entity
@Table(name = "CITY")
public class City {
@Id
@Column(name="city_ID")
@GeneratedValue
private int cityId;
@Column(name="city_name")
private String cityName;
// Getters and Setters
控制器
@RequestMapping(value="addstudent", method = RequestMethod.GET)
public String addStudent(@ModelAttribute("secret") Student student, BindingResult result, Model map)
{
map.addAttribute("student", new Student());
List<City> myCities = subService.getCity();
map.addAttribute("city", myCities);
return "addStudent";
}
@RequestMapping(value="addstudent", method = RequestMethod.POST)
public String saveStudent(@ModelAttribute("secret") Student student, BindingResult result, Model map)
{
studentService.addStudent(student);
map.addAttribute("success", "Submitted");
return "msg";
}
StudentDaoImpl
Session session = getSessionFactory().openSession();
Transaction tx;
tx = session.beginTransaction();
session.save(student);
tx.commit();
我的表格:addStudent.jsp
<c:url var="saveUrl" value="/addstudent" />
<form:form modelAttribute="secret" method="POST" action="${saveUrl}">
<table>
<tr>
<td><form:label path="studentName">Student Name:</form:label></td>
<td><form:input path="studentName"/></td>
</tr>
<tr>
<td><form:label path="studentDetails[0].fatherName">Father Name:</form:label></td>
<td><form:input path="studentDetails[0].fatherName"/></td>
</tr>
<tr>
<td><form:label path="studentDetails[0].motherName">Mother Name:</form:label></td>
<td><form:input path="studentDetails[0].motherName"/></td>
</tr>
<tr>
<td><label>City</label></td>
<td><form:select path="studentDetails[0].city">
<c:forEach var="row" items="${city}">
<option value="${row.cityId}">${row.city}</option>
</c:forEach>
</form:select>
</td>
</tr>
</table>
<input type="submit" value="Save" />
</form:form>
您在'學生'中設置'cityName'而不是'City'的'id' – 2014-09-11 12:44:04