-->
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 Question
PostPosted: Fri May 23, 2008 4:57 pm 
Newbie

Joined: Fri May 23, 2008 4:39 pm
Posts: 3
I need a little help with a mapping. Here are my tables and current mappings.

Teams:
TeamId


TeamGroups:
TeamGroupId
TeamId


TeamGroupMembers:
TeamGroupId
UserId

TeamGroupTeams
TeamGroupId
TeamId


If I want to get a specific team group its not a problem, but if I want to limit the number of teams that appear ( for pagination ) it won't work.
I think because hibernate doesn't exactly know how to go about it.

My TeamGroup mapping is:

<set name="teams" inverse="true" table="team_group_teams" >
<key>
<column name="team_group_id"></column>
</key>
<many-to-many class="Team" />
</set>
<set name="members" inverse="true" table="team_group_members" >
<key>
<column name="team_group_id"></column>
</key>
<many-to-many class="Users" />
</set>

So doing a:
Criteria crit = this.getSession().createCriteria(TeamGroup.class);
crit.setMaxResults(10);

Only knows to limit the number of TeamGroups returned, but not the
number of teams that belong to the group, which is what i'm after really.

What i'm trying to accomplish over all here is someone asks for a
group of teams ( like a division, or league ), and then I can
give the user a paginated list of those teams.

The feature is new so if there is a suggestion that means changing the table structure in some way I can do it without too much fuss.

I did my due diligence as far as researching the problem, but I haven't had much luck, and I've spent way too long on this as it is.

Any help would be great! Thanks so much!


Top
 Profile  
 
 Post subject: I fixed it
PostPosted: Sat May 24, 2008 6:49 pm 
Newbie

Joined: Fri May 23, 2008 4:39 pm
Posts: 3
Well I fixed it. Not sure if its the best way or not but I'll share just in case it can help someone else. Bummer nobody responded at all to my post... not even to ask for more info. Any way here goes:

I put teamGroups in the teams class / mapping:

Code:
<set name="teamGroups" table="team_group_teams">
      <key column="team_id"/>
      <many-to-many class="com.ack.dreamjournal.bo.TeamGroup" column="team_group_id" />
</set>


Now I do a criteria query on the teams class instead of the
teamGroups class so the setMaxResults will limit what I want.

Code:
      
Criteria c = this.getSession().createCriteria( Team.class);
        c.createAlias("teamGroups", "dg");
   c.add(Restrictions.eq("dg.id", dg.getId() ));
   c.setFirstResult( startPage * teamsPerPage);
   c.setMaxResults(teamsPerPage);


The createAlias is needed so hibernate will join correctly.

It took 30 seconds to write the sql query to give me the results, and
about 5 hours in all to figure out how to make hibernate do it.

Ugh...


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 2:47 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
We all share your paint. :(

Thanks so much for posting back. Your hard work will inevitably help out someone else having the same problems. Good Karma to you!

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.