-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many associations, composite id and order of columns
PostPosted: Thu Feb 26, 2004 4:24 pm 
Newbie

Joined: Thu Feb 26, 2004 2:29 pm
Posts: 1
I searched a bit (not absolutely throughly, I must confess ...) and didn't find it in the docs, the FAQs or the history, so I thought I would leave a note to those that might stumble in the same problem ...

I had a quite difficult to debug problem with a many-to-many association between two tables, where one of them had a composite-id (with an external class, but that is unimportant). The problem came from the fact that the order of the columns in the composite key table was different from the order of the columns in the relation table.

I'll try to show it with an example.

In the database, de tables are:

TableA: (idA, ...) pk: idA
TableB: (idB1, idB2, ...) pk: idB2, idB1
TableAB: (idB2, idB1, idA)pk: idB2, idB1, idBA

The mapping were:

Code:

<class
    name="br.com.tableA"
    table="tableA"
>

    <id
        name="idA"
        type="java.lang.Integer"
        column="id_usuario"
   unsaved-value="null"
    >
        <generator class="sequence">
            <param name="sequence">seq_A</param>
        </generator>
    </id>
   
    <property .../>
...
    <set
        name="bViaAB"
        lazy="true"
        table="tableAB"
    >
        <key>
            <column name="idA" />
        </key>
        <many-to-many
            class="br.com.tableB"
        >
[b]            <column name="idB2" />
            <column name="idB1" />
[/b]        </many-to-many>
    </set>
...
</class>


<class
    name="br.com.tableB"
    table="tableB"
    mutable="true"
>
   
    <composite-id name="comp_id" class="br.com.tableBPK">
[b]        <key-property name="idB1" column="id_cifra_servico" type="java.lang.String" />
        <key-property name="idB2" column="id_plataforma" type="int" />
[/b]    </composite-id>   
    <property ... />
...
</class>


And that gave me errors (constraint exceptions) every time I tried to insert new elements in the collection, on tableA's side.

When I changed tableB's the mapping to the following, everything worked just fine:

Code:
<class
    name="br.com.tableB"
    table="tableB"
    mutable="true"
>
   
    <composite-id name="comp_id" class="br.com.tableBPK">
[b]        <key-property name="idB2" column="id_plataforma" type="int" />
        <key-property name="idB1" column="id_cifra_servico" type="java.lang.String" />
[/b]    </composite-id>   
    <property ... />
...
</class>


Conclusion, in the many-to-many clause, the order of the column elemnts is important! Columns were not searched by name. I have to agree that my case is not very current, but any way, it happened so I thought it was worth mentioning.

Notes: Hibernate 2.1.2, PostgreSQL 7.3.4, hbm's generated with Middlegen.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 27, 2004 5:11 am 
Beginner
Beginner

Joined: Tue Jan 27, 2004 8:33 am
Posts: 29
I think the order is the only thing Hibernate can use to associate a column to a key in many-to-many relationship with a composite-id.
Maybe should be pointed out in the docs, though.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 01, 2004 11:58 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Correct, I've added it into the FAQ, though

_________________
Emmanuel


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