-->
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: many-to-many association between 2 tables with extra columns
PostPosted: Wed Nov 08, 2006 12:49 pm 
Beginner
Beginner

Joined: Fri Aug 20, 2004 3:45 pm
Posts: 22
Location: Florence - ITALY
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 08, 2006 5:59 pm 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Bidirectional navigation is not supported with this kind of navigation!

_________________
Please don't forget to rate


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.