限制Hibernate(和JPA)标准的表现力? [英] Limitation on the expressiveness of Hibernate (and JPA) Criteria?
问题描述
我在这样的层次结构中有4个实体:
I have 4 entities in a Hierarchy like this:
Parent
|
------------------------
| | |
Child1 Child2 Child3
我有兴趣检索Child1和Child2的所有实例(但不是Child3实例)具有单个查询中的条件。
And I am interested in retrieving All the instances of Child1 and Child2 (but not Child3 instances) with a criteria in a single Query.
这对HQL(或JPQL)来说很容易,如下所示: / p>
This is easy to do with HQL (or JPQL) as follows:
FROM Parent obj
WHERE obj IN (FROM Child1 where fieldOfChild1="aa") OR
obj IN (FROM Child2 where fieldOfChild2=55)
我研究了Hibernate Criteria和JPA Criteria API,我找不到一种方式来表达此查询作为条件。
I have studied the Hibernate Criteria and JPA Criteria APIs and I cannot find a way to express this query as a Criteria.
这是Criteria API的限制吗?还是只是我错过了路?任何提示?
Is this a limitation of Criteria APIs? or is it just that I missed the way? Any hint?
推荐答案
如果您的映射中的每个子类有一个鉴别器值(例如,对于Child1为1,对于Child2为2 ,3为Child3),您可以使用HQL / Criteria中的特殊属性class。
If you have an discriminator value for each subclass in your mapping (eg. 1 for Child1, 2 for Child2, 3 for Child3) you can use the special attribute "class" in your HQL/Criteria.
因此,标准查询可能如下所示:
So a criteria query could be like this:
session.createCriteria(Parent.class)
.add(Restrictions.or(Restrictions.eq("class", 1),
Restrictions.eq("class", 2)))
您的HQL查询简化: / p>
And your HQL query simplified:
FROM parent WHERE class = 1 OR class = 2
这篇关于限制Hibernate(和JPA)标准的表现力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!