This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Parent Key Not Found Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Parent Key Not Found Error" Watch "Parent Key Not Found Error" New topic
Author

Parent Key Not Found Error

Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Hi,

I have one-to-many Parent child relationship, When ever i try to
insert a record i get the following error "parent key not found".
Your suggestions are most welcome.

Following are my configuration files.

Parent xml file
Child

<?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 package="com.starwood.valhalla.inventory.hbtest">

<class name="ProductCategoryLookup"
table="PRDT_CATG_LKUP" >

<id name="categoryId" type="long">
<column name="PRDT_CATG_ID" length="15" not-null="true"/>
</id>


<property name="categoryParentId">
<column name="PRDT_CATG_PARENT_ID" length="15" />
</property>

<property name="categoryName">
<column name="PRDT_CATG_NAME" length="60" />
</property>

<property name="categoryDescription">
<column name="PRDT_CATG_DESC" length="260" />
</property>


<many-to-one
name="productCategoryOwner"
column="PRDT_CATG_OWNER_ID"
class="ProductCategoryOwner"
not-null="true"/>

</class>
</hibernate-mapping>


SCJP,SCWCD,SCBCD,SCEA Part I
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
mappings seem fine; can we see insert code?


java amateur
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Hi Miguel,

Following is my code.

Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Additional Information.

In PRDT_CATG_OWNER (Parent table)
prdt_catg_owner_id is the primary key

In PRDT_CATG_LKUP (Child table)
prdt_catg_id is the primary key
and prdt_catg_owner_id is foreign key.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
you create parent and child; set parent in child; add child to parnt's hashSet and then saveOrUpdate parent

all that looks ok

where exactly do you get the error msg?
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
just a guess: try instead saveOrUpdate, instead of just save()
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
hi miguel,

Actually when i try to do the save, two insert statements are executed
one on the parent and other on the child.

I think the child query is executing first that's why it could not
find the parent.

It is weird and i am confused about the order of execution.

Thanks for your suggestions, but i dont think saveorupdate will help.

Regards
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
in HiA (p 70) the order is:
new parent
new child
child.setParent(parent)
parent.getChildren().add(child)
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
I dont know why i am encoutering the error still,
Is something to do with hibernate version.

I am using hibernate3, any thoughts are suggestions are most
welcome.
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Hi Miguel,

Whatever i have done was correct,

I overlooked a self referencing foreign key in the child table(prod_catg_lkup).

Now i have solved the problem.

Thanks for your suggestions.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
glad to know

I overlooked a self referencing foreign key in the child table(prod_catg_lkup).

do you hand code your database?
Sujatha Kumar
Ranch Hand

Joined: Jan 05, 2004
Posts: 134
Yes i did a handcode of the configuration files.
miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281

i mean database: create table blabla
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Parent Key Not Found Error
 
Similar Threads
Retereive data by ORDER BY
Hibernate - Force foreign key lookup on insert
One-to-many mapping and composite key
Hibernte will delete rows from a table, but it won't insert rows
Cascading non foreign key to Child Table