-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many-To-Many with additional columns
PostPosted: Thu Mar 29, 2007 5:38 am 
Newbie

Joined: Tue Mar 20, 2007 7:51 am
Posts: 3
Hi,

I was wondering if what I'm trying to achieve is possible, and if so... how?

Basically I have a many-to-many mapping between two objects, relating to three tables (one for each of the classes, and a third to link the two together).

However, the third linking table needs to have additional values (namely last_updated_by and last_updated_date).
i.e. TBL_A_TO_B needs 2 more columns in addition to the 2 keys.

How would I go about modelling these extra columns so that they can be updated??

Any help much appreciated!!
Paul

Code:
<class name="my.package.ClassA" table="TBL_A">
    <id name="aId" type="big_decimal">
        <column name="A_ID" precision="22" scale="0" />
        <generator class="assigned" />
    </id>
    <property...
    ...property>
    <set name="bs" table="TBL_A_TO_B">
        <key>
            <column name="A_ID" precision="22" scale="0" not-null="true" />
        </key>
        <many-to-many entity-name="my.package.ClassB">
            <column name="B_ID" precision="22" scale="0" not-null="true" />
        </many-to-many>
    </set>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 6:06 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Well, it's simple : when you've got a central entity with properties, you don't define a many-to-many anymore, but a collection (see which one you want to use) of this central entity in the two ends of the relation.

From the central entity, map a <many-to-one /> relationship to both entities and you're done.

See the Java Persistence with Hibernate book if you need more details (p.303).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 6:16 am 
Newbie

Joined: Tue Mar 20, 2007 7:51 am
Posts: 3
OK, I see what you're saying, but if I'm understanding correctly, then I will end up with methods to get the links (central objects) rather than the other entity.

So rather than having A.getBs(), I will have A.getABLinks() (returning a new class with the setLastUpdatedDate and setLastUpdatedBy methods) and then need to call getB on each of the link objects.

Is that the way it needs to be done?
Cheers


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 29, 2007 6:34 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
pjhepworth wrote:
So rather than having A.getBs(), I will have A.getABLinks() (returning a new class with the setLastUpdatedDate and setLastUpdatedBy methods) and then need to call getB on each of the link objects.

Is that the way it needs to be done?
Cheers

Yes, exactly. Apart from the fact that you can decide to expose or not publicly visible methods to set the central additional property (lastUpdatedDate here). In fact, you could even map it with access="field" and write the code inside this object instead of managing it externally if you prefer.

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


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