-->
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.  [ 5 posts ] 
Author Message
 Post subject: mapping 3 tables linked with each other - error on save
PostPosted: Sun Feb 05, 2006 9:48 pm 
Newbie

Joined: Sun Feb 05, 2006 8:56 pm
Posts: 9
Hi,

I can't seem to map properly 3 objects/tables with cascades, and I get an error when I try to save the top level object. I have already spent more then a few hours trying to figure this one out by myself and I would greatly appreciate some help on the issue.

To make matters simple, I will call the objects A, B and C and the tables a, b, and c.

Here is a simple drawing of how the tables are linked:
http://img2.uploadimages.net/show.php?i ... ernate.jpg

each A can have one or more Bs or Cs, each B can have one or more Cs.

Hibernate version:
2

Mapping documents:

Code:
<class name="A" table="a">
        <id name="idA" column="id_a" type="java.lang.Long">
                <generator class="identity"/>
        </id>
        <!-- some other fields that do not matter here -->
        <set name="bSet" table="b" cascade="all-delete-orphan" inverse="true" >
           <key column="id_a" />
           <one-to-many class="B"/>
        </set>
      <set name="cSet" table="c" cascade="all-delete-orphan" inverse="true" >
           <key column="id_a" />
           <one-to-many class="C"/>
        </set>
</class>

<class name="B" table="b">
        <id name="idB" column="id_b" type="java.lang.Long">
                <generator class="identity"/>
        </id>
        <!-- some other fields that do not matter here -->
        <many-to-one name="a" class="A" column="id_a" />
      
        <set name="cSet" table="c" cascade="all-delete-orphan" inverse="true">
                <key column="id_b" />
                <one-to-many class="C" />
        </set>
</class>

<class name="C" table="c">
        <id name="idC" column="id_c" type="java.lang.Long">
                <generator class="identity"/>
        </id>
        <!-- some other fields that do not matter here -->
        <many-to-one name="a" class="A" column="id_a" />
        <many-to-one name="b" class="B" column="id_b" />
</class>



Code between sessionFactory.openSession() and session.close():
I only call saveOrUpdateCopy() on an object of type A

Full stack trace of any exception that occurs:
could not insert: [C] - this beeing the package and name of the class C
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:62)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1331)
...
Cannot insert the value NULL into column 'id_b', table 'c'; column does not allow nulls. INSERT fails.
...
net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)


I could post the generated SQL and complete stack trace, but I don't think that would help more. I am pretty sure I am doing something wrong when mapping the tables.
I am in desperate need of help, cause I've been stuck on this and I just can't seem to find the answer.
Thank you very much.

PS: I really do need cascade="all-delete-orphan" cause it would be hell if I had to save all the objects by myself. But if that is not possible, I would be glad for any solution to this problem.
Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 1:15 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
the cannot insert null problem in 1 to manies is fixed in hibernate 3. If that is your only problem, try upgrading.

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 6:20 am 
Newbie

Joined: Sun Feb 05, 2006 8:56 pm
Posts: 9
I have other 1-to-many objects that work just fine. For example I do have more then one of the following type of relationship between objects:

X ---- 1 to many ----- Y
Y ---- 1 to many ----- Z

I did not have a problem with those. But trying to add a
X ---- 1 to many ----- Z
that I can't manage to do.

If this is indeed a Hibernate bug, can you please provide a link to the bug description and possible workarounds ?
Upgrading to Hibernate 3 might not be possible to do mid-project, as there are things that might be affected by this change.

Thank you again


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 11:56 am 
Senior
Senior

Joined: Sat Jul 17, 2004 5:16 pm
Posts: 143
First of all Im not exactly sure it will fix your problem, but I had a similar problem and it fixed it. Try making the column nullable in the DB and see if hibernate is inserting null, then changing it to the ID in a subsequent update. Maybe this is something else. Also, if it is a foreign key constraint, try disabling the foreign key.

Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 12:28 pm 
Newbie

Joined: Sun Feb 05, 2006 8:56 pm
Posts: 9
Quote:
try disabling the foreign key

no can do.
the foreign key is there for a reason.
i will try to make the column nullable and see if hibernate is doing this in 2 steps. i will let you know how this went.

thanks for trying to help.
apparently nobody else encountered the problem and/or solved it. or ... they just don't want to share the solution with me :P


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