-->
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.  [ 7 posts ] 
Author Message
 Post subject: middlegen does not respect composite column ordering
PostPosted: Wed Mar 10, 2004 6:06 am 
Newbie

Joined: Wed Mar 10, 2004 5:28 am
Posts: 2
Hi, I'm using Hibernate 2.1.2 and the latest release of middlegen with postgres 7.4.1. I have had query generation problems in hibernate with composite keys, and the solution is clearly stated in the Hibernate FAQ http://www.hibernate.org/74.html#A3 -- columns for composite keys need to be in the same order inside composite-id, set, and many-to-one tags. Middlegen does not produce such universal orderings; once I hand-corrected my middlegen-generated XML files to respect such ordering my query problems disappeared.

I've taken a look at the hibernate.vm file in the middlegen CVS tree, and sure enough in composite-id tags an algorithm of non-relationship columns following each set of columns in relationships determines the ordering. For set and many-to-one tags, the built-in columnMaps of roles are used, and doesn't use the same more-complex ordering policy used in composite-id.

One solution would involve adding some mechanism that orders columns from columnMaps in roles just like the composite-id ordering that happens in hibernate.vm, but that would inelegantly put an ordering algorithm in Java that would need to maintain consistency over time with the state of the vm file. yuck. The alternative of doing extra work in composite-id to agree with columnMap orderings is complicated by ordering constraints in the tag structure. Anyone with better knowledge of the code have a an idea for a good fix? I've posted my middlegen hbm.xml files below -- the problem is that CUDestinations composite-id tag first mentions destinationcomponentid and then componentupdateid, while elsewhere componentupdateid precedes destionationcomponentid. Thanks!

-alan

PS. I think the real answer to this problem is that Hibernate shouldn't be so picky about column ordering, since it can reorder columns into SQL queries at will, but in the meantime, middlegen should deal appropriately...

--------------

ComponentUpdate.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

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

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

<class
name="context.arch.logging.hibernate.ComponentUpdate"
table="componentupdate"
>

<id
name="componentupdateid"
type="java.lang.Integer"
column="componentupdateid"
>
<generator class="assigned" />
</id>

<property
name="componentid"
type="java.lang.String"
column="componentid"
not-null="true"
length="-1"
/>
<property
name="updatetime"
type="java.sql.Timestamp"
column="updatetime"
not-null="true"
length="8"
/>
<property
name="updatename"
type="java.lang.String"
column="updatename"
not-null="true"
length="-1"
/>

<!-- associations -->
<!-- bi-directional one-to-many association to CUDestination -->
<set
name="CUDestinations"
lazy="true"
inverse="true"
>
<key>
<column name="componentupdateid" />
</key>
<one-to-many
class="context.arch.logging.hibernate.CUDestination"
/>
</set>

</class>
</hibernate-mapping>


CUDestinations.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

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

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

<class
name="context.arch.logging.hibernate.CUDestination"
table="cudestinations"
>

<composite-id name="comp_id" class="context.arch.logging.hibernate.CUDestinationPK">
<key-property
name="destinationcomponentid"
column="destinationcomponentid"
type="java.lang.String"
length="-1"
/>
<!-- bi-directional many-to-one association to ComponentUpdate -->
<key-many-to-one
name="ComponentUpdate"
class="context.arch.logging.hibernate.ComponentUpdate"
>
<column name="componentupdateid" />
</key-many-to-one>
</composite-id>

<property
name="success"
type="java.lang.Boolean"
column="success"
length="1"
/>

<!-- associations -->
<!-- bi-directional one-to-many association to CUAttribute -->
<set
name="CUAttributes"
lazy="true"
inverse="true"
>
<key>
<column name="componentupdateid" />
<column name="destinationcomponentid" />
</key>
<one-to-many
class="context.arch.logging.hibernate.CUAttribute"
/>
</set>

</class>
</hibernate-mapping>


CUAttributes.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

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

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

<class
name="context.arch.logging.hibernate.CUAttribute"
table="cuattributes"
>

<composite-id name="comp_id" class="context.arch.logging.hibernate.CUAttributePK">
<key-property
name="attributename"
column="attributename"
type="java.lang.String"
length="-1"
/>
<!-- bi-directional many-to-one association to CUDestination -->
<key-many-to-one
name="CUDestination"
class="context.arch.logging.hibernate.CUDestination"
>
<column name="componentupdateid" />
<column name="destinationcomponentid" />
</key-many-to-one>
</composite-id>

<property
name="attributetype"
type="java.lang.String"
column="attributetype"
not-null="true"
length="-1"
/>
<property
name="attributevaluestring"
type="java.lang.String"
column="attributevaluestring"
length="-1"
/>
<property
name="attributevaluenumeric"
type="java.lang.Float"
column="attributevaluenumeric"
length="4"
/>

<!-- associations -->

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 6:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
This is a known issue. I have to re-work the way Middlegen generates the ordering of the output. I have played with two solution approaches - still deciding what i'm going to do. You can change the order of the fields in your schema so the relationships are first - not nice - I agree. It will be fixed very soon.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 6:23 am 
Newbie

Joined: Wed Mar 10, 2004 5:28 am
Posts: 2
cool, thanks! Changing column ordering in my schema is actually not a bad idea right now and it didn't occur to me; for my app it's something I'll be able to do relatively painlessly.

-alan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 6:04 am 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
Search rocks!

I was just bit by the same problem. Using middlegen to create my .hbm.xml files, and the column orderings in the various mappings files were mixed around.

In my case, it was manifesting itself during a cascading insert, where one of the child tables was putting key values into the wrong columns (tring to cram a String into a Date).

Making the relative ordering the same in all the files seems to have fixed it. For reference, my tables are keyed like:

T1: K1
T2: K1, K2, K3
T3: K1, K2, K3, K4

2 hours down the drain on that problem. 5am and I've still not yet gotten Hibernate working end to end on a simple use case senario, although I'm getting close. I've got to get it nailed down today, before I unleash this on the rest of the developers. Damn deadlines. :)

Lotta work getting it going, but I'm gonna love it once it's ready.

--gus


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 7:39 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Glad the search worked for you. There is huge amount of information to be found in the forums.

Just to update those that are just now reading this thread;

Middlegen hibernate plugin (post R4) allows the developer to select the type of composite key generation mode. The default mode will always keep the keys in the correct order but if you prefer to have the key-many-to-one in the key itself then there is a risk that the ordering will not be correct.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 28, 2004 8:06 pm 
Beginner
Beginner

Joined: Thu Jun 24, 2004 1:04 pm
Posts: 35
Location: Minnesota - USA
Is "(post R4)" out yet?

I believe I'm using the hibernate plugin as packaged with Middlegen-2.0-vo, which from what I've read elsewhere has led me to believe it is R4.

--gus


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 29, 2004 9:48 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Middlegen 2.0 (vo) is R4.

I am about to release R5 but I keep wanting to add one more item :-)
Soon I hope.


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