How to add Distinct in Hibernate Criteria

前端 未结 8 1619
耶瑟儿~
耶瑟儿~ 2020-11-29 03:58

In my database I have a Test table, with columns: testName, testType there are 2 different tests with the same type I.e \"SUN\", so I want only one of them for which I use

8条回答
  •  醉话见心
    2020-11-29 04:02

    Use Projections.distinct.

    Criteria crit = session.createCriteria(Test.class).setProjection(
        Projections.distinct(Projections.projectionList()
        .add(Projections.property("type"), "type") )
    .setResultTransformer(Transformers.aliasToBean(YourBean.class)); 
    
    List lst = crit.list();
    

    where YourBean.class has a property "type". The returned list will be List.

提交回复
热议问题