0

Im using Criteria and joined one table having many to many mapping. When I left join with that table it returns duplicate entries.

I used all possible ways to distinct the result but its not working.

Session session = HibernateSessionFactory.getSession();
Criteria criteria = session.createCriteria(A.class, "a");
criteria.createAlias("a.b", "b", JoinType.LEFT_OUTER_JOIN);
criteria.addOrder(Order.asc("a.id");
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);

I have used ProjectionList also but its not working for me. It always give duplicate users.

Please suggest me what should I use if its many to many mapping between A and B and I want distinct A i.e user if it belongs to many B i.e. groups

snieguu
  • 2,073
  • 2
  • 20
  • 39
Vinay Sharma
  • 325
  • 4
  • 16
  • 1
    check https://stackoverflow.com/questions/25536868/criteria-distinct-root-entity-vs-projections-distinct ... – DeiAndrei Nov 24 '17 at 12:54
  • @DeiAndrei I want for Many to Many mapping criteria. I tried out that but it is showing duplicate still – Vinay Sharma Nov 24 '17 at 13:19
  • did you try with Projections.distinct()? – DeiAndrei Nov 24 '17 at 13:22
  • Yes. Its showing me org.postgresql.util.PSQLException saying SELECT DISTINCT, ORDER BY expressions must appear in select list – Vinay Sharma Nov 24 '17 at 13:28
  • I want to order by other property name say email. That was only one error appeared now. @DeiAndrei Projections.distinct() works now. But please help me how to resolve order by other property name? Since showing me **SELECT DISTINCT, ORDER BY expressions must appear in select list** – Vinay Sharma Nov 24 '17 at 13:43

0 Answers0