• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hibernate - insert object failed because of foreing key violation

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all -
I have a a table events that generates it's primary key. This table has another table (insertion_string) that associated to it with a foreign key. My DB is postgres.

<hibernate-mapping>
<class name="com.oversi.generated.generatedFiles.Events" table="events" schema="public">
<id name="eventId" type="long">
<column name="event_id" />
<generator class="sequence">
<param name="sequence">events_event_id_seq</param>
</generator>
</id>
<set name="insertionsStrings" inverse="true" cascade="all-delete-orphan" fetch="select">
<key>
<column name="event_id" not-null="true" />
</key>
<one-to-many class="com.oversi.generated.generatedFiles.InsertionsString" />
</set>



When I try to insert a new event object I get the error :

insert or update on table "insertions_string" violates foreign key constraint "insertions_string_event_id_fkey"
Detail: Key (event_id)=(0) is not present in table "events".

I think this is because hibernate try to insert to the foreign key table (insertion_string) before inserting to the events table ( the one with the primary key)
Please help me to solve this
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You, try putting the inverse="true" into the InsertionsString side of the relationship.

This is a very common issue that everyone seems to have happen to them. It seems to be more logical that the inverse would be on the one side of a many to one, but to get what we expect, parents getting inserted before their children, requires the inverse="true" to be on the Many side of the relationship.

If you think of it this way, wherever you put the inverse, Hibernate will think, ok, so this is the same relationship that is modeled in the other side, and lets use that mapping as the controlling force of this relationship. So by having it in the Many side, it puts the parent in control of the relationship, since after all it is responsible for creating its children. Unless it is by immaculate conception.

Mark
[ April 17, 2008: Message edited by: Mark Spritzler ]
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic