如何使用条件Api检索集合属性 [英] How can I retrieve a collection property using criteria Api
本文介绍了如何使用条件Api检索集合属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用条件
public class A {
private Collection<B> property
// getters and setters
}
public class B {
private int status
// getters and setters
}
我的标准代码如下:
Criteria cr = getSession().createCriteria(A.class)
cr.createAlias("property", "prop")
cr.add(Restrictions.eq("prop.status", status));
cr.setProjection(Projections.property("prop"));
cr.list();
很明显,此代码不起作用,我只是想证明我的意图。我知道如何使用HQL实现此目的,但是我必须使用Criteria API。我的目标是什至可以使用Criteria吗?
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 ?
推荐答案
不幸的是,使用Hibernate Criteria无法实现我想要的目标。如果有人需要这样的东西,您应该创建一个namedQuery,它可能对您来说太可怕了,或者只是使用hql即可。
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
这篇关于如何使用条件Api检索集合属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文