-->
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.  [ 11 posts ] 
Author Message
 Post subject: insert object failed because of foreing key violation
PostPosted: Wed Apr 16, 2008 6:19 am 
Newbie

Joined: Mon Feb 25, 2008 1:14 pm
Posts: 8
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.

Code:
<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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 8:56 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
...that's strange...I know it sounds self-evident but just to be certain...are you sure you don't try to save an insertions_string object before the call to save the event object??


mosa


Top
 Profile  
 
 Post subject: I'm not trying to save those objects twice
PostPosted: Thu Apr 17, 2008 3:21 am 
Newbie

Joined: Mon Feb 25, 2008 1:14 pm
Posts: 8
I checked it in my code to be sure. The Exception happened in the commit time - any idea?


Top
 Profile  
 
 Post subject: Re: I'm not trying to save those objects twice
PostPosted: Thu Apr 17, 2008 6:45 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
leel wrote:
I checked it in my code to be sure. The Exception happened in the commit time - any idea?


do you by any chance set the parent of the child explicitly??


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 7:11 am 
Newbie

Joined: Mon Feb 25, 2008 1:14 pm
Posts: 8
I am sorry - but I did not understand your message.
What is the meaning
Quote:
do you by any chance set the parent of the child explicitly


The xml of the insertionString:
Code:
   <class name="com.oversi.generated.generatedFiles.InsertionsString" table="insertions_string" schema="public">
        <composite-id name="id" class="com.oversi.generated.generatedFiles.InsertionsStringId">
            <key-property name="eventId" type="long">
                <column name="event_id" />
            </key-property>
            <key-property name="index" type="short">
                <column name="index" />
            </key-property>
        </composite-id>
        <many-to-one name="events" class="com.oversi.generated.generatedFiles.Events" update="false" insert="false" fetch="select">
            <column name="event_id" not-null="true" />
        </many-to-one>
        <property name="insertionStr" type="string">
            <column name="insertion_str" not-null="true" />
        </property>
    </class>

The code to save the object:

Code:
event.setServerName("arak");

InsertionsString newInsertionStr = new InsertionsString();
newInsertionStr.setId(newInsertionsStringId((long)event.getId(),1));
newInsertionStr.setInsertionStr("kkkk");
newInsertionString.setEvents(event);

Set <InsertionString> insertionStrSet = new HashSet();
insertionStrSet.add(newInsertionStr);

event.setInsertionString(insertionStrSet);

session.saveOrUpdate(event);
session.commit();


Is something wrong with this code?


Last edited by leel on Thu Apr 17, 2008 9:45 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 8:21 am 
Beginner
Beginner

Joined: Wed Mar 05, 2008 4:57 am
Posts: 22
Location: Bangalore,India
remove the inverse=true from the mapping. It should work.

_________________
Naresh Waswani
+91-9986461501


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 17, 2008 8:28 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
leel wrote:
I am sorry - but I did not understand your message.
What is the meaning
Quote:
do you by any chance set the parent of the child explicitly


The xml of the insertionString:
Code:
   <class name="com.oversi.generated.generatedFiles.InsertionsString" table="insertions_string" schema="public">
        <composite-id name="id" class="com.oversi.generated.generatedFiles.InsertionsStringId">
            <key-property name="eventId" type="long">
                <column name="event_id" />
            </key-property>
            <key-property name="index" type="short">
                <column name="index" />
            </key-property>
        </composite-id>
        <many-to-one name="events" class="com.oversi.generated.generatedFiles.Events" update="false" insert="false" fetch="select">
            <column name="event_id" not-null="true" />
        </many-to-one>
        <property name="insertionStr" type="string">
            <column name="insertion_str" not-null="true" />
        </property>
    </class>

The code to save the object:

Code:
event.setServerName("barak");

InsertionsString newInsertionStr = new InsertionsString();
newInsertionStr.setId(new InsertionsStringId((long)data.getId(),1));
newInsertionStr.setInsertionStr("kkkk");

Set <InsertionString> insertionStrSet = new HashSet();
insertionStrSet.add(newInsertionStr);
insertionString.setEvents(event);
session.saveOrUpdate(event);
session.commit();


Is something wrong with this code?


sorry, my bad....

what I mean is that from what I can also see in your code the parent is not being set the "Set" of children.
For instance, I think that there should somewhere be a call to event.seItnsertionStrSet(insertionStrSet) or something similar.


mosa


Top
 Profile  
 
 Post subject: Thank you for your answer
PostPosted: Thu Apr 17, 2008 9:42 am 
Newbie

Joined: Mon Feb 25, 2008 1:14 pm
Posts: 8
1. I delete the inverse = true from my code - but it did not help
2. I do combined between the parent to the child (I just forgot to wrote it in my code - silly me)

any other idea - please?
please?


Top
 Profile  
 
 Post subject: Re: Thank you for your answer
PostPosted: Thu Apr 17, 2008 10:04 am 
Beginner
Beginner

Joined: Tue Feb 26, 2008 2:04 pm
Posts: 28
Location: UK
leel wrote:
1. I delete the inverse = true from my code - but it did not help
2. I do combined between the parent to the child (I just forgot to wrote it in my code - silly me)

any other idea - please?
please?


hi,

I believe the "inverse=true" should be used in most cases for optimized DML, otherwise hibernate will generate unnecessary (at least one) SQL statements....

regarding your problem may I also ask what is the Java type you have used in your Event class?? I see you map it as a hibernate long but is it a primitive long or wrapper Long ?? I'm just wandering because if you use a primitive type the jvm will always assign a value of zero for the identifier and it won't be ignored by hibernate unless you use an unsaved-value property.


mosa


Top
 Profile  
 
 Post subject: I solve the problem
PostPosted: Mon Apr 21, 2008 5:17 am 
Newbie

Joined: Mon Feb 25, 2008 1:14 pm
Posts: 8
The problem was that the foreign key in the insertion string table was also part of the primary key in it (I have composite id in insertion string table)
thank you all for trying to help


Top
 Profile  
 
 Post subject: Re: I solve the problem
PostPosted: Tue Sep 16, 2008 1:59 am 
Newbie

Joined: Tue Sep 16, 2008 1:51 am
Posts: 1
leel wrote:
The problem was that the foreign key in the insertion string table was also part of the primary key in it (I have composite id in insertion string table)
thank you all for trying to help


Hi Leel,

I'm struggling with the same problem, and finally find your post.
my problematic class has also a foreign key which is part of the primary key.
I could not understand what's wrong with it? why is it a problem? and what is your solution?

I'll really appriciate an answer.
thanks
Eran


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.