Hibernate加入两个表

时间:2014-09-10 14:38:54

标签: java hibernate spring-mvc

我正在尝试学习Spring和Hibernate。我有以下表格

enter image description here

在我的数据库表中插入表单值后,我希望它们如下所示:

表名:学生

student_id    studentName
  1.           Jason Stathum

表名:studentdetails

studentDetailsid   FatherName   MotherName    student_id
   1                 Mr.X          Mrs. Y        1

但是当我实际在我的数据库中插入值时,studentdetails表看起来像是

表名:studentdetails

studentDetailsid   FatherName   MotherName    student_id
   1                 Mr.X          Mrs. Y        NULL

正如您所看到的,一切都运行良好,但只有student_id列不会被填满。你能告诉我我做错了吗?

这是我的代码:

模特课程:学生

package com.spring.org.model

@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, mappedBy = "student")
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
@JoinColumn(name="student_id")
private Student student;

// 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());     
    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";

}

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>
</table>

 <input type="submit" value="Save" />
</form:form>

StudentDaoImpl

@Override
public void addStudent(Student student) {

    Session session = getSessionFactory().openSession();
    Transaction tx;
    tx = session.beginTransaction();
    session.save(student);  

    tx.commit();
}

3 个答案:

答案 0 :(得分:0)

@JoinColumn中的student属性中删除StudentDetails注释应该可以解决问题:

@ManyToOne
//@JoinColumn(name="student_id")
private Student student;

答案 1 :(得分:0)

更改此片段:

@ManyToOne
@JoinColumn(name="student_id")
private Student student;

为:

@ManyToOne
private Student student;

这应该有效。

答案 2 :(得分:0)

我设法解决了这个问题,希望有一天能帮到别人。 我在

中做了一些改动

模特课程:学生

@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

然后从Model Class:StudentDetails中删除private Student student;属性。所以现在它看起来像这样:

模型类:StudentDetails

@Entity
@Table(name = "studentDetails")
public class StudentDetails {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer studentDetailsId;
private String FatherName;
private String MotherName;

// Getters and Setters