-->
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: Multiple cascadings problem
PostPosted: Thu Apr 27, 2006 10:03 pm 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
I'm trying to cascade class1 with a one-to-many relationship to class2 which, in turn, has a many-to-one relationship with class3.

The problem is that class2 has a composite key containing the keys from class1 and class3 and the key from class3 is auto-generated.

This is the error I get:
#23000Cannot add or update a child row: a foreign key constraint fails (`supremacy/gameuser`, CONSTRAINT `FK_gameuser_game` FOREIGN KEY (`gameid`) REFERENCES `game` (`gameid`))

here are my mapping files:

<class name="Class1, DataAccess" table="1">

<id name="ID1" column="userid" type="Int32">
<generator class="identity" />
</id>

<bag name="class2list" inverse="true" cascade="all" table="tbl1tbl3">
<key column="id1"/>
<one-to-many class="class1class3, DataAccess"/>
</bag>
</class>

<class name="class1class3, DataAccess" table="tbl1tbl3">

<composite-id unsaved-value="any">
<key-property name="ID1" column="id1" type="Int32"/>
<key-property name="ID3" column="id3" type="Int32"/>
</composite-id>

<many-to-one name="Class3Obj" class="Class3, DataAccess" cascade="all" column="id3" insert="false" update="false" />
</class>

<class name="Class3, DataAccess" table="tbl3">
<id name="ID3" column="id3" type="Int32" unsaved-value="-1">
<generator class="identity" />
</id>
</class>

And here is the code that I'm using to try to do this:

Class3 new3 = new Class3();

Class13 new13 = new Class13();
new13.Class3obj = new3;
Current1.class21list.Add(new13);


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 27, 2006 10:20 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
I am pretty sure the fields used in the composite key have to be nullable. NH first tried to delete the child objects and then recreates them. even the ones you haven't changed. check your log files for NH to see exactly what is going on. you can also search here on composite keys for more info.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 12:09 pm 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
That didn't work. the problem as I see it is that new3 needs to be saved before new13 but that isn't happening. Also, I forgot to include that I'm doing this in a transaction. Does it matter if I try to do this using Session.Save() or transaction.Commit()?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 12:32 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
I just noticed that you set the insert/update in the many-to-one from class1class3 to class3 as false. From thedocs:

Quote:
Setting both (insert/update) to false allows a pure "derived" association whose value is initialized from some other property that maps to the same colum(s) or by a trigger or other application.


In this instance you would manually have to save the class3 object first. Why are you setting those values to false?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 12:37 pm 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
Because when I set them to true it says that for a many-to-one class you have to set them to false


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 12:48 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
Okay, I'm reaching here because I've not heard or seen that before. I have many m-1 relatinships with setting those values, but not inside composite-id classes. However, going over the HiA book, it does state specifically:

Quote:
If a class with a natural (composite) key does not declare a version or timestamp property, it's more difficult to get SaveOrUpdate() and cascades to work properly.


So you might want to add that field to your class. Also, you need to be sure you're implementing Equals() and GetHashCode() which I'm sure you are.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 2:24 pm 
Newbie

Joined: Mon Apr 10, 2006 11:45 pm
Posts: 17
So is that essentially saying I need to make a unique key for that table (such as a version or timestamp) and then just make the other fields foreign keys?

When I change those to "true" I get this error:

Repated column in mapping for class Class13 should be mapped with insert="false" update="false": ID3


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 2:36 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
well actually, if you have the option, in my opinion, you should never use natural (composite keys). So, yes, if you can, create a surrogate key and then PK/FK the two opbects you are trying to relate:

Code:
<class name="Class13">
    <id name="id" column="surrogateid">
        <generator class="identity" />
    </id>

    <many-to-one name="Class1" class="Class1" column="ID1" />
    <many-to-one name="Class3" class="Class3" column="ID3" />
</class>


-devon


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.