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.