简体   繁体   English

混合的投影

[英]hiebrnate projection with distinct

I am trying to get the distinct on my column "testType" in table "Test". 我试图在表“ Test”的列“ testType”上获得唯一性。 Also I want to get both "testType" and "testId" in my list. 我也想在列表中同时获取“ testType”和“ testId”。 For that I am doing this but it doesn't applying the distinct , giving me the duplicate result. 为此,我正在这样做,但是没有应用distinct,给了我重复的结果。

Any idea how can I resolve this? 知道我该如何解决吗?

Session session = sessionFactory.openSession();
Criteria crit = session.createCriteria(Test.class);
ProjectionList proList = Projections.projectionList();
proList.add(Projections.property("testType"));
proList.add(Projections.property("testId"));
crit.setProjection(proList);
crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List rsList = crit.list();

Thanks in advance. 提前致谢。

to get the distinct values of testType 获得testType的不同值

Session session = sessionFactory.openSession();
List results = session.createCriteria(Test.class);
    .setProjection(Projections.distinct(Projections.property("testType")))
    .list();

but to get the id as well you can't select for it since there may be many ids for each Type, eg: 但是要获取ID,您也不能选择它,因为每种类型可能有很多ID,例如:

testId | testType
-----------------
1      | type1
2      | type2
3      | type1
4      | type2
6      | type3
7      | type3

what should Select Distinct(testType), testid return? Select Distinct(testType), testid返回什么?

  1. (type1, 1), (type2, 2), (type3, 6) (type1,1),(type2,2),(type3,6)
  2. (type1, 3), (type2, 4), (type3, 7) (type1、3),(type2、4),(type3、7)
  3. (type1, 1), (type2, 4), (type3, 6) (type1,1),(type2,4),(type3,6)

1) 1)

Session session = sessionFactory.openSession();
List results = session.createCriteria(Test.class);
    .setProjection(Projections.projectionList()
        .add(Projections.group(Projections.property("testType"))))
        .add(Projections.Min(Projections.property("testid")))))
    .list();

2) 2)

Session session = sessionFactory.openSession();
List results = session.createCriteria(Test.class);
    .setProjection(Projections.projectionList()
        .add(Projections.group(Projections.property("testType"))))
        .add(Projections.Max(Projections.property("testid")))))
    .list();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM