-->
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: Mapping associations
PostPosted: Wed Apr 25, 2007 11:33 am 
Newbie

Joined: Fri Mar 02, 2007 5:01 am
Posts: 6
Hello. I'm newbie with hibernate. I'm trying to map an association but I'm not sure how to do. I have 3 tables:

USERS( id_usr, name, email....): P.K.: id_usr
USERS_ITEMS( id_usr, id_item, amount): P.K.: (id_usr, id_item)
ITEMS( id_item, name ): P.K.: id_item

As you can see an user can be related with many ITEMS (relation USERS_ITEMS) and an item can be related with many USERS. What I need to do is from Users java class obtain its related ITEMS adding the amount column. I don't know how to perform this relation in the mapping files.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 25, 2007 1:31 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
This a special many-to-many case, 'cause there's an additional column. So just create an additional central entity class (or component, that's your prerogative), then map a one-to-many from both sides to this central class.

See reference documentation for this: http://www.hibernate.org/hib_docs/v3/re ... -onetomany or the Java Persistence with Hibernate book.

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


Top
 Profile  
 
 Post subject: I think this is not the solution
PostPosted: Thu Apr 26, 2007 10:27 am 
Newbie

Joined: Fri Mar 02, 2007 5:01 am
Posts: 6
Hi. Thanks for your answer. I can't do that, because there isn't any foreign key from USERS to USERS_ITEMS. USERS_ITEMS have a foreign key to USERS and another one to ITEMS. These foreign keys compose the primary key of USERS_ITEMS. What I want, is from USERS obtaining the ITEMS asociated an adding the amount column in USERS_ITEMS. Let's look an example:

USERS
---------
id_usr | name.....
----------------
00001 | XXXX

ITEMS
-------
id_item | name.......
-----------------
item001| aaaa
item002| bbbb

USER_ITEMS
------------------------------
is_usr | id_item | amount
-----------------------------
00001 | item001| 10
00001 | item002| 25

From an user I'd like to get this:

item001 | aaaa | 10
item002 | bbbb | 25

Do you understand?? At the moment, I've mapped this in USERS.hbm.xml:

<set name="items" table="USER_ITEMS">
<key column="id_usr"/>
<many-to-many column="id_item" class="Item" />
</set>

This association returns the items related to an user, but I need the amount property/column too. The relationship between Users and User_items is 0..n. So, I've thought obtaining a collection of user_items, and then, with each user_items instance, obtain the item. How can I do than?


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.