Hibernate criteria projection distinct
Asked Answered
A

1

21

Hi i want to write a query using criteria : The following query has to be created using criteria:

"Select Distinct(s2Taxper) from S2 where s2Tc='601' AND s2Txcd!=''"

thanks in advance

Aldana answered 20/8, 2012 at 5:43 Comment(0)
P
44
Criteria criteria = 
    session.createCriteria(S2.class)
           .add(Restrictions.eq("s2Tc","601"))
           .add(Restrictions.ne("s2Txcd",""))
           .setProjection(Projections.distinct(Projections.property("s2Taxper")));
Pagan answered 20/8, 2012 at 5:55 Comment(3)
projection returns array of objects ( Object[] ) , is there any way to get object of custom classes even after using projection? like in the above answer if I want list<S2> then ?Nescience
We can get by the below code. criteria.setResultTransformer(new AliasToBeanResultTransformer(S2.class));Pagan
I got this error: ERROR [http-nio-8443-exec-8] SqlExceptionHelper.logExceptions(146) | ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list after using setProjection().Fafnir

© 2022 - 2024 — McMap. All rights reserved.