Wednesday, 15 September 2010

java - Cascade not working in hibernate properly -



java - Cascade not working in hibernate properly -

in below given code, cascade="save-update", used course of study class(bag) associated pupil class.

student class is-->

private int id; private string firstname; private string lastname; private address address; private list<course> courses;

course class is-->

private int id; private string name; private int unit;

hbm file pupil is-->

<?xml version="1.0"?> <!doctype hibernate-mapping public "-//hibernate/hibernate.mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="pojos.student" table="student"> <id name="id" type="integer" column="id"> <generator class="increment" /> </id> <property name="firstname"> <column name="first_name" /> </property> <property name="lastname"> <column name="last_name" /> </property> <many-to-one name="address" class="pojos.address" column="address_id" cascade="save-update" /> <bag name="courses" inverse="true" cascade="save-update"> <key column="student_id" /> <one-to-many class="pojos.course" /> </bag> </class> </hibernate-mapping>

transaction i'm performing simply-->

pupil stud = new student("ketan", "dikshit"); address address = new address("dm-road", "uttar pradesh", "201301"); course of study course1 = new course("core java", 101); course of study course2 = new course("advanced java", 201); list<course> courses = new arraylist<course>(); courses.add(course1); courses.add(course2); stud.setaddress(address); stud.setcourses(courses); seek { tr = session.begintransaction(); system.out.println("\n transaction has begun..!!"); session.save(stud); tr.commit(); system.out.println("\n transaction commit..!!"); session.close(); system.out.println("\n session closed..!!"); } grab (exception e) { system.out.println("\n transaction in errror..!!"); tr.rollback(); system.out.println("\n transaction rollback..!!"); session.close(); system.out.println("\n session closed..!!"); }

now,as per code, courses table should holding column named "student_id" primary key of associated pupil nowadays each entry in course of study table.but column "student_id" not showing data( null values inserted).

the query sequence is-->

transaction has begun..!! hibernate: select max(id) pupil hibernate: select max(id) address hibernate: select max(id) course of study hibernate: insert address (street, city, zipcode, id) values (?, ?, ?, ?) hibernate: insert pupil (first_name, last_name, address_id, id) values (?, ?, ?, ?) hibernate: insert course of study (name, unit, id) values (?, ?, ?) hibernate: insert course of study (name, unit, id) values (?, ?, ?) transaction commit..!!

why column in course of study table showing null values, instead of holding id's student??

remove inverse="true" handbag mapping otherwise owner of relation course of study , not student.

java mysql hibernate hbm

No comments:

Post a Comment