-->
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.  [ 5 posts ] 
Author Message
 Post subject: Association size limit
PostPosted: Tue Oct 26, 2010 3:11 pm 
Newbie

Joined: Mon Jun 05, 2006 11:44 am
Posts: 11
Here's my example:
[*]Categories which have 1-n association to Products (most likely m-n association but that's not important--I don't think)
[*]Categories also have 1-{0..10} association to "Top 10" Products which may or may not be the same as Product association above

This way you could do:

lazy init
Code:
Category cat = getCategory(catId);
cat.getProducts(); // to get the 1-n products
cat.getTopTenProducts(); // to get the "Top Ten" products


-or-
eager fetching
Code:
select c from Category c left join fetch c.products p left join fetch c.topTenProducts ttp


My question is: Is it possible to have size-bound associations like this in Hibernate? Ideally, association would be both size-bound and have other additional criteria that defines the association.


Top
 Profile  
 
 Post subject: Re: Association size limit
PostPosted: Wed Oct 27, 2010 9:19 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
There are a lot of solutions for such problem. The easiest one is to only map "getProducts()" in Hibernate and implement "getTopTenProducts()" in such a way that it will return ten of them.

If this is not OK to you, you can always have native SQL in mapping (see formula column for more details) or view that defines needed association properly.


Top
 Profile  
 
 Post subject: Re: Association size limit
PostPosted: Wed Oct 27, 2010 11:19 am 
Newbie

Joined: Mon Jun 05, 2006 11:44 am
Posts: 11
Quote:
The easiest one is to only map "getProducts()" in Hibernate and implement "getTopTenProducts()" in such a way that it will return ten of them.


Won't this require me to load ALL product associated to a category when all I want is 10? If a category has thousands of products, it seems like a waste to have to load all of them just to turn around and only use 10.

The ultimate aim here is I'd like to do something like the following:

Code:
select c from Category c left join fetch c.topTenProducts


And get back a list of Category objects all with their top ten products pre-loaded without having Hibernate load ALL of the products for each category.


Top
 Profile  
 
 Post subject: Re: Association size limit
PostPosted: Thu Oct 28, 2010 10:12 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
Make a custom view then.


Top
 Profile  
 
 Post subject: Re: Association size limit
PostPosted: Thu Oct 28, 2010 11:00 am 
Beginner
Beginner

Joined: Tue Oct 26, 2010 6:12 pm
Posts: 29
Another solution would be to load the association based on custom SQL using the loader element in your hbm file. The custom SQL could limit the number of elements you need depending on a parameter.


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