public List<String> listOfChunks() {
Criteria criteria = getSession().createCriteria(Student.class);
criteria.addOrder(Order.asc("id"));
criteria.setProjection(Projections.sqlProjection("right(name,1) as lastChar", new String[]{"lastChar"}, new Type[]{ new StringType()}));
return criteria.list();
}
上面的查询给了我所有学生姓名的最后一个字符,我怎么能避免在list()中得到的lastchar中的重复?
答案 0 :(得分:1)
使用Projections.distinct从投影中创建不同的投影
Criteria criteria = getSession().createCriteria(Student.class);
criteria.addOrder(Order.asc("id"));
criteria.setProjection( Projections.distinct(
Projections.sqlProjection("right(name,1) as lastChar"
, new String[]{"lastChar"}
, new Type[]{ new StringType()})));
return criteria.list();