2014-09-11 82 views
0

我正在使用Hibernate和Spring MVC。我有以下形式ManyToOne - 保存時未設置外鍵

enter image description here

當我提交我想我的數據庫表來填補像下面的形式:

表名:學生

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> 
+0

您在'學生'中設置'cityName'而不是'City'的'id' – 2014-09-11 12:44:04

回答

0

我在這裏猜測,但是這可能是因爲你在輸入記錄時沒有真正選擇一個城市,而只是輸入了一個學生姓名,一個父親姓名和一個母親姓名,並且把城市留在了第一個城市在你的名單上,這恰好是「紐約」。也許你可以添加一個額外的「請選擇」選項,用一個驗證器強制你選擇一個城市。

此外,我注意到您在某些地方使用city_id,而在其他地方使用city_ID。你的數據庫對列名不區分大小寫嗎?

+0

感謝您的回覆。我嘗試了額外的「請選擇」選項,但它沒有奏效。我用'city_ID'來引用我的'city'table,我用'city_id'作爲'studentDetails'表中的外鍵列。我認爲我的數據庫對列名不區分大小寫。謝謝 – 2014-09-11 14:31:04

0

我已經解決了這個問題:)我在我的HTML表單中做了一些改變,現在它的工作。

<tr> 
    <td><label>City</label></td> 
    <td><form:select path="studentDetails[0].city.cityId"> 
     <c:forEach var="row" items="${city}"> 
      <option value="${row.cityId}">${row.cityName}</option>   
     </c:forEach> 
     </form:select> 
    </td> 
</tr>