I am trying to learn Spring and Hibernate . I have the following form
After inserting the form values in my database tables, I want them to look like following:
Table Name : student
student_id studentName
1. Jason Stathum
Table Name : studentdetails
studentDetailsid FatherName MotherName student_id
1 Mr.X Mrs. Y 1
But when I actually insert values in my database, the studentdetails
table looks like following
Table Name : studentdetails
studentDetailsid FatherName MotherName student_id
1 Mr.X Mrs. Y NULL
As you can see everything works perfectly but only the student_id
column doesn't get filled up. Could you please tell me what I am doing wrong?
Here's are my codes:
Model Class : Student
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
Model Class : StudentDetails
@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
Controller
@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 Page : Form
<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();
}
Removing the @JoinColumn
annotation from the student
attribute in StudentDetails
should fix the issue:
@ManyToOne
//@JoinColumn(name="student_id")
private Student student;
Change this fragment:
@ManyToOne
@JoinColumn(name="student_id")
private Student student;
to:
@ManyToOne
private Student student;
and this should work.
I have managed to solve the problem, I hope this will help others someday. I made some changes in my
Model Class: Student
@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
And then removed the private Student student;
property from Model Class: StudentDetails. So now it looks like this:
Model Class : 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.