java - Hibernate joining two tables -
i trying learn spring , hibernate . have following form
after inserting form values in database tables, want them 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 insert values in database, studentdetails
table looks following
table name : studentdetails
studentdetailsid fathername mothername student_id 1 mr.x mrs. y null
as can see works student_id
column doesn't filled up. please tell me doing wrong?
here's 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 , 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 , 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 @joincolumn
annotation student
attribute in studentdetails
should fix issue:
@manytoone //@joincolumn(name="student_id") private student student;
Comments
Post a Comment