-->
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.  [ 4 posts ] 
Author Message
 Post subject: FetchMode.JOIN returning too many entities
PostPosted: Wed Aug 23, 2006 4:00 am 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
core: 3.2.0.cr2, ann: 3.2.0.CR1

hi,

I have a problem with a Criteria query returning the same entity multiple times in a result list. Given the entity structure below with each item having an arbitrary number of translations and item groups. If I then use the following query setting the associations to be fetched eagerly I get a resultset containing too many Items.

Code:
Criteria criteria = session.createCriteria(Item.class).
   add(Restrictions.eq("someProperty", propertyValue))
   .setFetchMode("itemGroups", FetchMode.JOIN)
   .setFetchMode("translations", FetchMode.JOIN);
List lst = criteria.list();


For each Translation and ItemGroup entity assigned to an Item a fully fetched Item is returned. This means that the same entity is contained multiple times in the returned list! The number of returned items actually corresponds to #(Translations) x #(ItemGroups) but with each item having the association sets fully and correctly initialised.

e.g. If I have an item with a german and an english translation + 3 sub groups assigned the query returns 6 entities instead of just one!

This only happens if I eagerly fetch the associations. I furthermore tried to realise the same query using HQL only to reach the same problem...

Any comments are highly appreciated!

thx,

john


Entities:
Code:
@Entity
public class Item {
   private Long itemId;
   private Set<ItemTranslation> translations;
   private Set<ItemGroup> itemGroups;
   private String someProperty;

   @OneToMany(targetEntity=ItemTranslation.class, mappedBy="translatedItem", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
   public Set<ItemTranslation> getTranslations() {...}

   @ManyToMany(targetEntity=ItemGroup.class, fetch=FetchType.LAZY)
   public Set<ItemGroup> getItemGroups() { ...}
}

@Entity
public class ItemTranslation {
   private Long id;
   private String translation;
   private Locale locale;
   private Item translatedItem;
}

@Entity
public class ItemGroup {
   private Long id;
   private String name;
}

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 4:05 am 
Newbie

Joined: Thu May 04, 2006 5:04 am
Posts: 17
Location: Austria
Call method setResultTransformer() on your criteria object prior to execution with argument CriteriaSpecification.DISTINCT_ROOT_ENTITY.

See the Criteria API for further informations about the different modes:

CriteriaSpecification.ROOT_ENTITY
CriteriaSpecification.DISTINCT_ROOT_ENTITY
CriteriaSpecification.ALIAS_TO_ENTITY_MAP

[url]http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/CriteriaSpecification.html#DISTINCT_ROOT_ENTITY
[/url]
kind regards, Chris

_________________
Please don't forget to rate the posting if it helped you, thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 4:39 am 
Beginner
Beginner

Joined: Sat Apr 15, 2006 12:49 pm
Posts: 20
Location: austria
Thanks chris, that solved the problem for the criteria API!

But is there a way to do the same with the HQL?

And furthermore, as this is an essential point when using eager fetching in combination with a query, I'm really wondering why I could not find a word about it in der hibernate documentation regarding the FetchMode property
( http://www.hibernate.org/hib_docs/v3/re ... icfetching ).


regards,
john


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 23, 2006 5:31 am 
Newbie

Joined: Thu May 04, 2006 5:04 am
Posts: 17
Location: Austria
Hi,
sorry but I don't use HQL statements in our application for this sort of things.
We are only using it for simple finder methods (no joins involved).
But I found the hint that you can specify the join type which hibernate should use in HQL.
Something like
Quote:
from eg.Cat as cat
inner join cat.mate as mate
left outer join cat.kittens as kitten

from eg.Cat as cat left join cat.mate.kittens as kittens

from Formula form full join form.parameter param

Maybe this does the same thing as the fetchmode in criteria API.

http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html

kind regards, Chris

_________________
Please don't forget to rate the posting if it helped you, thanks


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