From Hibernate Tips&Trick:
I have a many-to-many association between two tables, but the association table has some extra columns (apart from the foreign keys). What kind of mapping should I use?
Use a composite-element to model the association table. For example, given the following association table:
Code:
create table relationship (
fk_of_foo bigint not null,
fk_of_bar bigint not null,
multiplicity smallint,
created date )
you could use this collection mapping (
inside the mapping for class Foo):
Code:
<set name="relationship">
<key column="fk_of_foo"/>
<composite-element class="Relationship">
<property name="multiplicity" type="short" not-null="true"/>
<property name="created" type="date" not-null="true"/>
<many-to-one name="bar" class="Bar" not-null="true"/>
</composite-element>
</set>
But, what about Bar side of relation? I need a getFoo() in Bar class that return all Foo related to this Bar.
My solution is:
Code:
<set name="relationship">
<key column="fk_of_bar"/>
<composite-element class="Relationship">
<property name="multiplicity" type="short" not-null="true"/>
<property name="created" type="date" not-null="true"/>
<many-to-one name="foo" class="Foo" not-null="true"/>
</composite-element>
</set>
I don't think this works, bacause using Hibernate tools this cause the following warning message:
Code:
[hibernatetool] INFO Version(<clinit>:15) - Hibernate Tools 3.2.0.beta8
[hibernatetool] WARN ConfigurationNavigator(addComponent:110) - Component it.dp.magazzino.beans.Consistenza found more than once! Will only generate the last found.
Obviously, the class
Consistenza is the "relationship" entity in my project.
Thank to all.
Gianni