Criteria.list()中的重复项

时间:2014-11-28 07:21:09

标签: hibernate

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中的重复?

1 个答案:

答案 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();