Hibernate Criteria查询以获取特定列结果

时间:2013-11-14 05:40:22

标签: hibernate criteria

我尝试使用Criteria获得单列结果,但我没有得到该

的完美输出
Criteria criteria =  session.createCriteria(GroupName.class)
        .setProjection(Projections.property("groupName"));
        criteria.createAlias("gcmUsers", "user").setResultTransformer(
                Criteria.DISTINCT_ROOT_ENTITY);
        criteria.add(Restrictions.eq("user.clientName", userName));
        groupNames=criteria.list();

 i am getting this output :

Default, Default, Default, Default, Default, Default, twiter_grp


but my actual output is :
Default, twiter_grp,friends

1 个答案:

答案 0 :(得分:1)

您可以使用Projections.distinct方法

Criteria criteria =  session.createCriteria(GroupName.class)
        .setProjection(Projections.distinct(Projections.property("groupName")));
        criteria.createAlias("gcmUsers", "user").setResultTransformer(
                Criteria.DISTINCT_ROOT_ENTITY);
        criteria.add(Restrictions.eq("user.clientName", userName));
        groupNames=criteria.list();

More about projections distinct method