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

user1450954
  • 237
  • 1
  • 3
  • 10

1 Answers1

44
Criteria criteria = 
    session.createCriteria(S2.class)
           .add(Restrictions.eq("s2Tc","601"))
           .add(Restrictions.ne("s2Txcd",""))
           .setProjection(Projections.distinct(Projections.property("s2Taxper")));
Chance
  • 11,043
  • 8
  • 61
  • 84
Ramaraj Karuppusamy
  • 2,350
  • 5
  • 24
  • 35
  • 1
    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 then ? – Sadanand Oct 28 '14 at 09:49
  • We can get by the below code. criteria.setResultTransformer(new AliasToBeanResultTransformer(S2.class)); – Ramaraj Karuppusamy Nov 18 '14 at 14:09
  • 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(). – Khasan 24-7 Nov 20 '15 at 04:35