-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to map auto generated id to a child table as foriegn key
PostPosted: Fri Jan 12, 2007 10:10 am 
Newbie

Joined: Fri Jan 12, 2007 9:47 am
Posts: 1
This is my problem:

I have a Parent table and a child table. I have Set object in my master table DTO class as to refer the my child table.

E.g
public class Parent {
Set childrent;
...
...

}

This is my parent table hibernate mapping file

<hibernate-mapping >

<class name="Parent" table="PARENT">

<id name="id" type="long" unsaved-value="0">
<column name="ID"/>
<generator class="native">
<param name="sequence">SEQ_ID</param>
</generator>
</id>


<set name="children" table="CHILD" lazy="true" cascade="all">
<key column="Parent_ID"/>
<one-to-many class="Child"/>
</set>

</class>

</hibernate-mapping>

This is my child table hibernate mapping file

<hibernate-mapping >

<class name="Child" table="CHILD">

<id name="childId" type="long" unsaved-value="0">
<column name="CHILD_ID"/>
<generator class="native">
<param name="sequence">SEQ_CHILD_ID</param>
</generator>
</id>


<property name="id" type="long">
<column name="ID" />
</property >

</class>

</hibernate-mapping>

I have made constraint that the ID column in the Child table is referenced by foriegn key ID of Parent table.

When I try to save Parent object through session, I am getting following exception.

org.springframework.dao.DataIntegrityViolationException:
Hibernate operation:Could not execute JDBC batch update;
SQL
[insert
into
CHILD
(CHILD_ID,
ID)
values
(?,
?];
ORA-02291:
integrity
constraint
(TEST.CHILD_FK1)
violated
-
parent
key
not
found ;
nested
exception
is
java.sql.BatchUpdateException:
ORA-02291:
integrity
constraint
(TEST.CHILD_FK1)
violated
-
parent
key
not
found at
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.translate(SQLErrorCodeSQLExceptionTranslator.java:235) at
org.springframework.orm.hibernate3.HibernateAccessor.convertJdbcAccessException(HibernateAccessor.java:424) at
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:411) at
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:363) at
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:595) at
com.openreach.portal.serviceaccess

Am I missing some thing?

I am using Oracle10g as DB and application server is Weblogic 8.1.

Appriciate your help.

Thanks & Regards,
Siraj


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 12, 2007 10:47 am 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
there's no Parent_ID column in the child class

edit:
and if it's auto-generated. You need to have a many-to-one from the child back to the parent. Hibernate will then know to use that auto-generated value in the child class as the FK constraint.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.