• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ally Cavs
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
reply
    Bookmark Topic Watch Topic
  • New Topic