-->
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: Simple (?) mapping question
PostPosted: Fri Jun 04, 2004 4:48 pm 
Newbie

Joined: Fri Jun 04, 2004 4:21 pm
Posts: 3
I have 3 tables that represent a many-many relationship.
User->Mapping>Item

Mapping is a simple table with 2 columns user_id and item_id and the generated class contains two instance variables that reference a User class and an Item class. My confusion is trying to determine how to query against the generated Mapping Class. Given a User, how can I get the current collection of Items via the Mapping? What I can't wrap my brain around is how to write a query to query the Mapping table. I guess I could refetch the User object (which has a collection of Mappings), but is that the right way?

Thanks,
Dennis


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 05, 2004 2:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
select i from Mapping as m join m.item as i where m.user.id = ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 05, 2004 12:33 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Or, perhaps Mapping should not be an actual class. If it has no use besides connecting User and Item, just use the mapping table to connect those two objects. In the User.hbm:

<set name="items" table="mappings" lazy="true" >
<key column="user_id"/>
<many-to-many column="item_id" class="com.company.model.Item"/>
</set>

And the reverse in Item.hbm.


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.