-->
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.  [ 2 posts ] 
Author Message
 Post subject: Parameters Reversed by Hibernate
PostPosted: Thu Jan 29, 2004 8:21 pm 
Newbie

Joined: Thu Dec 11, 2003 10:06 pm
Posts: 8
I have three classes/mappings and I have found that I am getting two params reversed on insert statements when using Hibernate. Here's the mappings

<!-- mapping 1 -->
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="TableA"
table="TABLE_A"
>

<id
name="aId"
type="long"
column="A_ID"
>
<generator class="assigned" />
</id>
<set
name="tableBs"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
>
<key>
<column name="A_ID" />
</key>
<one-to-many
class="TableB"
/>
</set>
</class>
</hibernate-mapping>

<! --- mapping 2 -->

<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="TableB"
table="TABLE_B"
>

<composite-id name="comp_id" class="TableBPK">
<key-property
name="extId"
column="EXT_ID"
type="int"
length="10"
/>
<!-- bi-directional many-to-one association to TableA -->
<key-many-to-one
name="tablea"
class="TableA"
>
<column name="A_ID" />
</key-many-to-one>
</composite-id>
<set
name="tableCs"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
>
<key>
<column name="A_ID" />
<column name="EXT_ID" />
</key>
<one-to-many
class="TableC"
/>
</set>

</class>
</hibernate-mapping>

<!-- mapping 3 -->
<hibernate-mapping>
<!--
Created by Middlegen Hibernate plugin

http://boss.bekk.no/boss/middlegen/
http://hibernate.sourceforge.net/
-->

<class
name="TableC"
table="TABLE_C"
>

<id
name="cId"
type="int"
column="C_ID"
>
<generator class="sequence">
<param name="sequence">c_id_seq</param>
</generator>
</id>
<many-to-one
name="tableB"
class="TableB"
not-null="true"
>
<column name="A_ID" />
<column name="EXT_ID" />
</many-to-one>

</class>
</hibernate-mapping>

<! -- End Mappings -- >

<!-- classes -->
class TableA
{
private Long aId;
private Set tableBs;
// getters, setters, constructors, and that's it
}

class TableB
{
private TableBPK comp_id;
private Set tableCs;
// getters, settters, constructor
}

class TableBPK
{
private int extId;
prvate TableA tableA;
// getters, setters, constructors
}

class TableC
{
private Integer cId;
private TableB tableB;
// getters, setters, constructor
}

All the mappings were generated by Middlegen and all the Java code was generated by hbm2java.

I had no problems with just Table A and Table B. When I added Table C and tried to save, I got a foreign key violation from the C->B relationship. I dropped the key constraint on C, and then the insert worked, but I realized that it had put the value for EXT_ID in A_ID, and the value for A_ID in EXT_ID. Hence the foreign key constraint. I thought that surely I had done something obviously wrong, causing these two values to be reversed, but I couldn't really see what would cause this. I tried reversing the order in the TableB mapping's set:key section, but to no avail. Any ideas what would cause these values to get placed incorrectly by inserts issued by Hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 29, 2004 10:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
This is an known problem with Middlegen :-(. It has to do with the order that the fields are generated by the Composite ID class in this case - Table B should have the key-many-to-one mapping before the key-property mapping as this is the natural order of the key. It sometimes comes out in the wrong order die to the key-properties getting priority over the key-many-to-one properties. I can see you have changed the mapping anyway so reverse this part of the mapping will fix the problem.


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