How to do a distinct count in JPA critera API?

后端 未结 4 1483
盖世英雄少女心
盖世英雄少女心 2021-02-13 10:14

I would like to do this but with the criteria API instead:

select count(distinct e) from Event e, IN(e.userAccessPermissions) p where p.principal = :principal an         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-13 10:27

    Like this?

    Criteria crit = session.createCriteria(Event.class):
    crit.createAlias("userAccessPermissions", "p");
    crit.add(Restrictions.eq("p.principal", principal);
    crit.add(Restrictions.in("p.permission", permissions);
    crit.setProjection(Projections.countDistinct("id"));
    

提交回复
热议问题