Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Object Relational Mapping
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Eclipse Collections Categorically: Level up your programming game
this week in the
Open Source Projects
forum!
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Object Relational Mapping
Child object is not saving.
pankaj semwal
Ranch Hand
Posts: 303
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have a screen where parent table has two child and having one to one mapping between parent and child's.
I want if parent is saved then child should automatically saved.But this not happening.
Where Student is parent and school and scast are its child.
Please help me....
Below are my HBM files.
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.Hibernate.Student" table="STUDENT" schema="SYSTEM"> <id name="studentId" type="java.lang.Integer"> <column name="STUDENT_ID" precision="22" scale="0" /> <generator class="sequence" > <param name="sequence">STUDENT_SEQ</param> </generator> </id> <property name="studentName" type="java.lang.String"> <column name="STUDENT_NAME" length="100" not-null="true" /> </property> <property name="studentAddress" type="java.lang.String"> <column name="STUDENT_ADDRESS" length="500" not-null="true" /> </property> <property name="studentDob" type="java.lang.String"> <column name="STUDENT_DOB" length="100" not-null="true" /> </property> </class> </hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.Hibernate.School" table="SCHOOL" > <id name="schoolId" type="java.lang.Integer"> <column name="SCHOOL_ID" /> <generator class="sequence" > <param name="sequence">SCHOOL_SEQ</param></generator> </id> <many-to-one name="student" class="com.Hibernate.Student" fetch="select" cascade="all" > <column name="SCH_STU_ID" precision="22" scale="0" unique="true"/> </many-to-one> <property name="schoolName" type="java.lang.String"> <column name="SCHOOL_NAME" length="100" /> </property> <property name="schoolAddress" type="java.lang.String"> <column name="SCHOOL_ADDRESS" length="100" /> </property> </class> </hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.Hibernate.StudentCast" table="SCAST" schema="SYSTEM"> <id name="studentCastId" type="java.lang.Integer"> <column name="STUDENT_CAST_ID" /> <generator class="sequence"> <param name="sequence">STUDENT_CAST_SEQ</param> </generator> </id> <many-to-one name="student" class="com.Hibernate.Student" fetch="select" cascade="all" > <column name="STUDENT_ID" precision="22" scale="0" unique="true"/> </many-to-one> </class> </hibernate-mapping>
Java
code
package com.Hibernate; import java.io.Serializable; import org.hibernate.Transaction; import org.hibernate.Session; public class Client { public static void main(String[] args) { Session session = HibernateSessionFactory.getSession(); Transaction transaction = session.beginTransaction(); Student student = new Student(); student.setStudentName("Pankaj"); student.setStudentAddress("South Delhi"); student.setStudentDob("18/12/1982"); School school = new School(); school.setSchoolName("KV"); school.setSchoolAddress("South School"); StudentCast studentCast = new StudentCast(); //studentCast.setStudent(student); student.setSchool(school); student.setStudentCast(studentCast); school.setStudent(student); session.save(school); transaction.commit(); } }
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How about saving the school object instead of student?
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
problem with hibernate
Hibernate how to update only one colum value
Exception when having different class and table name
Retereive data by ORDER BY
One-to-many relationship is updating entire child table with new parent id
More...