How can I retrieve a collection property using criteria Api

﹥>﹥吖頭↗ 提交于 2019-12-11 16:34:43

问题


I want to retrieve a collection property using criteria

   public class A {  
       private Collection<B> property  
       // getters and setters
   }  
   public class B {
      private int status
      // getters and setters
   }

My criteria code is as follows:

Criteria cr = getSession().createCriteria(A.class)     
cr.createAlias("property", "prop")
cr.add(Restrictions.eq("prop.status", status));
cr.setProjection(Projections.property("prop"));
cr.list();

It's obvious this code doesn't work I wanted to simply demonstrate my intentions. I know how to achieve this using HQL, but I have to use Criteria API. Is what I am aiming for even possible using Criteria ?


回答1:


What wrong with this solution ?

Criteria cr = getSession().createCriteria(B.class);
 cr.add(Restrictions.eq("status", status));
 cr.list(); 



回答2:


Unfortunately what I want to achieve is not possible with Hibernate Criteria. If someone needs something like that you should create a namedQuery , as awfull as that migh be to you or just use hql.

Peter



来源:https://stackoverflow.com/questions/3608220/how-can-i-retrieve-a-collection-property-using-criteria-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!