JProfiler
Get rid of your performance problems and memory leaks!
[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Win a copy of JBoss AS 5 Development this week in the JBoss forum
or Spring Dynamic Modules in Action in the Spring forum!
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
Author

left join fetch returns duplicates

Slava Lovkiy
Greenhorn

Joined: Aug 17, 2005
Messages: 29

I have Cat and Kitten entities, that are in many-to-one relationship (Cat has many kittens)
I need to return those Cats and only those Kittens of that Cat that matches certain conditions.

I.e.

from Cat cat
left join fetch cat.kittens kitten
where kitten.color = 'gray'


Using this hql i ran into the following problem:
If one Cat has N 'gray' kittens, I got N instances of the same Cat returned with N kittens pre-loaded in Cat.kittens collection.

If I will use 'left join' w/o 'fetch' I got correct number of Cats, but Cat.kittens now contains all kittens, including 'gray'.

The 'distinct' keyword doesnt work for me too.

Any idea how to avoid duplication in result list?
Christian Bauer
author
Ranch Hand

Joined: Aug 31, 2004
Messages: 45

Set resultSet = new HashSet( q.list() );

Co-Author of <a href="http://www.manning.com/bauer" target="_blank" rel="nofollow">Hibernate in Action</a>
Slava Lovkiy
Greenhorn

Joined: Aug 17, 2005
Messages: 29

It won't work correctly in case of pagination of results. Number of rows will be calculated before removing dups.
Christian Bauer
author
Ranch Hand

Joined: Aug 31, 2004
Messages: 45

You can't use pagination with an outer join fetch. If you start thinking about the SQL and how the resultset looks like, it should be quite obvious why it doesn't work.

Co-Author of <a href="http://www.manning.com/bauer" target="_blank" rel="nofollow">Hibernate in Action</a>
Slava Lovkiy
Greenhorn

Joined: Aug 17, 2005
Messages: 29

Indeed.

Looks like I will need to refactor query to not fetch 'one-to-many' assosiations in one select. However, is there any other ways to make Hibernate lazy loading of assosiated objects using some criteria restrictions ?
I found session.filter(collection, "hql") not suitable since it returns the subset of elemnts in list rather then list as property of selected object.
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Object Relational Mapping
 
RSS feed
 
New topic
replay challenge