dslevine wrote:
I want to have 'outer-join=true' in my mapping file, but I want to be able to use the Criteria API and not have it automatically join on all of those tables. (Or else I get many duplicates.)
Are you saying that for example, is it this case :
when you want to get a particular A that owns a collection of 4 B, your criteria.list() has 4 elements. These elements are all the same reference to the right A you want ?
If so, then this behaviour is normal. It is due to the underlying outer-join behaviour. Do the SQL manually, you should understand this. The Hibernate team decided not to remove the duplicates created by this kind of join for perfs.
So the logical workaround if you want to get objects once only (no duplicates) is for example to write a method that will delete dups, like this :
Code:
public static List removeDuplicate(List list)
{
return new ArrayList(new HashSet(list));
}
So, with Hibernate, now you'd write :
Code:
List myBeans = session.createCriteria(YourClass.class).add().....list();
myBeans = removeDuplicate(myBeans);
I didn't test it, but with Hibernate 3.2, retrieving "distinct" instances can be achieved. The thing is that it's with HQL, not with criteria as you want :
Code:
select distinct a from A a left join fetch a.b