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]