Hibernate / JPA:是否可以在单个查询中检索异构实体? [英] Hibernate/JPA: Is it possible to retrieve heterogeneous entities in a single query?
问题描述
我有2个实体:EntityA和EntityB。
它们是无关的,我不能将它们放在继承树中,以避开此问题范围之外的某些限制。
但是我需要在同一个JPQL或HQL查询中包含包含两个实体的所有实例的混合列表。这是可能的JPA甚至Hibernate直接?
我需要这样的事情:
FROM EntityA WHERE fieldA = 1
UNION
FROM EntityB WHERE fieldB =aa
任何提示?
嗯,我终于明白了。 b
它足以让实体实现一个通用接口(甚至不需要在Hibernate中声明该接口)。
然后,这样的查询可以完成:
FROM my.package.CommonInterface obj
WHERE obj IN(FROM EntityA WHERE fieldA = 1)OR
obj IN(FROM EntityB WHERE fieldB ='a')
这样,您检索 List< CommonInterface>
。
问题解决了。
I have 2 entities: EntityA and EntityB.
They are unrelated, and I cannot put them in a Inheritance tree for some restrictions out of the scope of this question.
But I need to get in the same JPQL or HQL query a mixed List containing all the instances of both entities. Is this possible with JPA or even Hibernate directly?
I need somethign like this:
FROM EntityA WHERE fieldA=1
UNION
FROM EntityB WHERE fieldB="aa"
Any hint?
Well, I finally figured it out.
It is enought to make the entities implement a common interface (it is not even needed to declare this interface on Hibernate).
Then, a query like this can be done:
FROM my.package.CommonInterface obj
WHERE obj IN (FROM EntityA WHERE fieldA=1) OR
obj IN (FROM EntityB WHERE fieldB='a')
This way, you retrieve a List<CommonInterface>
.
Problem solved.
这篇关于Hibernate / JPA:是否可以在单个查询中检索异构实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!