获取 testType 的不同值
Session session = sessionFactory.openSession();
List results = session.createCriteria(Test.class);
.setProjection(Projections.distinct(Projections.property("testType")))
.list();
但是要获取 id 也不能选择它,因为每种类型可能有很多 id,例如:
testId | testType
-----------------
1 | type1
2 | type2
3 | type1
4 | type2
6 | type3
7 | type3
应该Select Distinct(testType), testid
返回什么?
- (type1, 1), (type2, 2), (type3, 6)
- (type1, 3), (type2, 4), (type3, 7)
- (type1, 1), (type2, 4), (type3, 6)
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)
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();