ManyToOne - 保存时未设置外键

时间:2014-09-11 11:42:04

标签: java spring hibernate spring-mvc

我正在使用Hibernate和Spring MVC。我有以下表格

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

仅供参考,我还有一个名为city的表,如下所示

表名:studentdetails

city_ID  city_name
   1      NewYork

我在下面提供了我的代码,您能告诉我如何解决这个问题吗?

非常感谢

实体:学生

@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

实体:学生详细信息

@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>

2 个答案:

答案 0 :(得分:0)

我在这里推测,但可能是你在输入唱片时没有真正选择一个城市,而只是输入了学生姓名,父亲姓名和母亲姓名,然后离开了城市显示在您列表中的第一个城市,恰好是#34;纽约&#34;。也许你可以添加额外的&#34;请选择&#34;选项,带有强制您选择城市的验证器。

另外,我注意到你在某些地方使用city_id而在其他地方使用city_ID。您的数据库是否对列名称不区分大小写?

答案 1 :(得分: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>