1

I have an entity like this (any unnecessary code omitted):

class Client{
    private id;  
    @OneToMany(mappedBy = "client", fetch = FetchType.EAGER)
    private Set<Remark> remarks = new HashSet<Remark>();
    //getters and setters
}

I call List clients = session.createCriteria(Client.class). If Client has multiple remarks, I get multiple the same Client objects, for example if Client has 2 remarks, then in list there are two the same clients. I don't think this is good behaviour, am I missing something?

polmarex
  • 1,363
  • 5
  • 19
  • 35
  • 1
    It's difficult to determinate the problem with this block of code. Can you post the entire relation between Client and Remarks? and how you initialize the client list. – manix Sep 01 '12 at 00:18
  • This is expected behavior. See this post http://stackoverflow.com/questions/1995080/hibernate-criteria-returns-children-multiple-times-with-fetchtype-eager – nsfyn55 Sep 01 '12 at 00:31

2 Answers2

0

Unfortunately it seems that you have to use criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); to get what you want. Look at the link pasted by nsfyn55.

Łukasz Rzeszotarski
  • 5,791
  • 6
  • 37
  • 68
0

I doesn't look like you are using HQL but in the interest of completeness another way to solve this is using the distinct keyword in HQL. I am reasonably sure that an HQL query like the one below will also solve your problem.

select distinct c from Client c where c.id = ? 
nsfyn55
  • 14,875
  • 8
  • 50
  • 77