File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes insert new parent/child row. How to handle new primary/foreign key inserts 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 "insert new parent/child row. How to handle new primary/foreign key inserts" Watch "insert new parent/child row. How to handle new primary/foreign key inserts" New topic
Author

insert new parent/child row. How to handle new primary/foreign key inserts

Ally Cavs
Ranch Hand

Joined: Aug 25, 2008
Posts: 89
Hi
I have a parent object
Its primary key is auto increment field in MYSQL
This is simply parentID

now parent ID also exists in the child object as a foreign Key

So inserting Parent works fine
Inserting child has a blank parent ID in its foreign key field
I do not have foreign key constraint in place on the database as i would get a foreign key constraint error then

But how can I get Hibernate to insert a row, pick up on the new primary key, set that key in the child rows

my child rows are a List in the parenet. The list is set to cascade="all"

I am saving by
session.save(parent)
session.commit();

Can give working sample code or link to a comprehenise example please

Prashant R Kataria
Greenhorn

Joined: Jul 23, 2007
Posts: 20
can you send the snippest of your hbm files.

However, hibernate uses foreign key which maps has-a relationship in object model. So in order to have parent id in child class you will have to have a fk constraint on child table.


-
Prashant Kataria
SCJP 5.0

"If you are going through hell, keep going!!!"
Ally Cavs
Ranch Hand

Joined: Aug 25, 2008
Posts: 89
Thanks for the reply Prashant.

i will post up the XML later

So I need a foreign key constraint on teh child table. Thats fine I will do that
Does teh raltionship need to be

1)bi-directional
2)dpes the foreign-key="" attribute in the XML files have anything to do with it

Do i put the name of the foreign key constraint in here like foreign-key="sample_fk_constraint"
Prashant R Kataria
Greenhorn

Joined: Jul 23, 2007
Posts: 20
As far as relationship is concerned, Uni-Directional or Bi-direction is up to you and your requirements.

for one-to-one association between parent and child:

In parent class for child property:

<many-to-one name="childPropertyName"
class="ChildClassname"
column="CHILD_COLUMN_NAME_IN_DATABASE"
cascade="all"
unique="true"
/>

By doing this your parent class will have fk which will be primary key of child class.




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: insert new parent/child row. How to handle new primary/foreign key inserts
 
Similar Threads
Saving Parent having multiple Child row refering Multiple parents gives Foreign Key constrain
How to Determine Whether to Insert or Update
Deleting from multiple Tables
delete child rows using hibernate. (cascade?)
Hibernate MySQL Auto increment insert inc. field been a foreign key