-->
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.  [ 8 posts ] 
Author Message
 Post subject: create of children fails with a foreign key violation
PostPosted: Wed Oct 22, 2003 3:13 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
Hi,

i create a bo1 object and a buch of bo2 children from a transport object in one step (code below).

If i don't call Session.flush() before inserting the children, the insert of the children will fail due to a foreign key constraint in the database.

The code is simular to the example in the reference manual, the main difference is that parent and children are created in the same session.

The reason for the failure is in the nullifyTransientReferences() call in net.sf.hibernate.impl.SessionImpl, because the isUnsaved() method checks for e.existsInDatabase, which returns false, allthough the SQL insert call was made.

Code:
    // create the parent first
    Integer key= new Integer(pTO.getA());
    BO1     bo1= new BO1();
    bo1.setB(pTO.getB());
    pSession.save(bo1, key);

    // we must flush the session, otherwise the following inserts will fail
    pSession.flush();

    // create the children
    List ds= pTO.getD();
    for (int i = 0, n = ds.size(); i < n; i++)
    {
        String  d  = (String)ds.get(i);
        BO2     bo2= new BO2();
        bo2.setC(i);
        bo2.setD(d);
        bo1.addBO2(bo2);
    }


The addBO2 method in the BO1 class is
Code:
    public void addBO2(BO2 pBO2)
    {
        pBO2.setParent(this);
        mBO2s.add(pBO2);
    }



Bye,

J


Top
 Profile  
 
 Post subject: Re: create of children fails with a foreign key violation
PostPosted: Wed Oct 22, 2003 3:19 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
zet wrote:
Code:
        <many-to-one name="parent"  column="a" not-null="true"/>


This should be:
Code:
        <many-to-one name="parent" class="com.sdm.perftest.hibernate.BO1" column="a" not-null="true"/>

You left out the class?

-G


Top
 Profile  
 
 Post subject: Re: create of children fails with a foreign key violation
PostPosted: Wed Oct 22, 2003 3:31 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
Brannor McThife wrote:
zet wrote:
Code:
        <many-to-one name="parent"  column="a" not-null="true"/>


This should be:
Code:
        <many-to-one name="parent" class="com.sdm.perftest.hibernate.BO1" column="a" not-null="true"/>

You left out the class?

I added the class statement, but the behaviour didn't change:
Code:
java.sql.SQLException: General error, message from server: "Column 'a' cannot be null"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 4:02 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Perhaps more clearly define your ID's, including their types and unsaved values (for both).

Like this:
Code:
    <class name="com.sdm.perftest.hibernate.BO1" table="TAB1">
        <id name="a" column="a" type="int" unsaved-value="0">
             <generator class="native"/>
        </id>
...


and
Code:
    <class name="com.sdm.perftest.hibernate.BO2" table="TAB2">
        <id name="id" column="b" type="int" unsaved-value="0">
            <generator class="native"/>
        </id>
...



-G


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 4:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This problem occurs if one class is using "identity" (or "native") id generation and the other is not.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 5:24 am 
Beginner
Beginner

Joined: Wed Sep 24, 2003 3:14 am
Posts: 20
Location: Munich, Germany
gavin wrote:
This problem occurs if one class is using "identity" (or "native") id generation and the other is not.

Should i make a bug report for that issue?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 5:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
no. it is not a bug.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 22, 2003 5:53 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Zet, what's he's trying to say, in his unique way. :) Is that you should do like I showed, that if you're using "native", then you should do the same on the other side of the relationship.

-G


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