-->
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.  [ 2 posts ] 
Author Message
 Post subject: how to left join fetch and filter a collection?
PostPosted: Thu Aug 09, 2007 9:42 am 
Newbie

Joined: Thu Aug 09, 2007 8:29 am
Posts: 4
Can somebody teach me an approach to left join fetch and filter the collection being fetched. In my situation, I have 4 Hibernate mapped Java classes, and there is a one-to-many relation that goes down through all of them in a line. Each class is a collection of another class.

What I want to achieve is that when a collection is being fetched, not all the associated objects should be loaded, but only these meet the condition.

For example, There are Country,City,District, and Street 4 classes. A country has a collection of cities, a city has a collection of districts, and a district has a collection of streets. Now I want query Country with conditions being imposed on it's city,district and street, and the city,district,or street will not be fetched if it dosen't meet the condition. If There is not any city, district and street meet the conditions, their country will not be selected, otherwise the country will be selected, but of which maybe not all child cities, districts and streets be selected. This is what I mean filter the collection.

In the new version hibernate reference, I have noticed a keyword 'with'. It seems useful in my case. Can some one provide me some good suggestions.Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 09, 2007 6:59 pm 
Newbie

Joined: Thu Aug 09, 2007 8:29 am
Posts: 4
Let me say it in code, maybe it will facilitate you understanding my problem.

The data structure is:
Country --(collection)-->City--(collection)-->District--(collection)-->Street

The query is:
from Country as country
left join fetch country.cities as city
left join fetch city.districts as district
left join fetch district.streets as street
where country.name like 'A%'
and city.name like 'N%'
and district like 'M%'
and street like 'P%'

This query will return a list of Country objects. But there is a problem, when I access the collections, I found out thay are lazy loaded. When Hibernate lazy loads objects ,i.e. cities of a country, it will load all the cities of that country using a sql ,like 'select * from CITY_TABLE where COUNTRY_ID = ?'. And as I have explained, I don't want all the cities to be loaded, but only these that meet the condition , say, city's name with an inital N.

That's my problem. is there any solution or hibernate dose not support this kind of query?


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