How to add Distinct in Hibernate Criteria

前端 未结 8 1621
耶瑟儿~
耶瑟儿~ 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:22

    Had the same problem and ended up solving using the Group By projection and then adding in all the columns I needed. For example

    Criteria query = session.createCriteria(Class.class)
        .setProjection(Projections.projectionList()
            .add(Projections.groupProperty("Col1"), "Col1")
            .add(Projections.groupProperty("Col2"), "Col2"))
        .setResultTransformer(Transformers.aliasToBean(Class.class));
    List list =  query.list();
    

提交回复
热议问题