[英]Hibernate Criteria - multiple entity types in projection
I have two entities Category
, Subscriber
. 我有两个实体
Category
, Subscriber
。 Subscriber
has reference on Category
(there can be only one subscriber for given category ie 1-to-1). Subscriber
可以参考Category
(给定类别只能有一个订户,即一对一)。 In HQL I'm able to select all categories and it's subscriber (if any) by using LEFT JOIN
(projection: category, nullable subscriber ie SELECT category, subscriber
). 在HQL中,我可以使用
LEFT JOIN
选择所有类别及其订阅者(如果有的话)(projection:类别,可为空的订阅者,即SELECT category, subscriber
)。
I would like to rewrite this to Criteria
. 我想将此重写为
Criteria
。 Since I can navigate only from Subscriber
to Category
I have to use RIGHT JOIN
: 由于我只能从
Subscriber
导航到Category
我必须使用RIGHT JOIN
:
Criteria c = getSession()
.createCriteria(Subscriber.class, subscriberAlias)
.createAlias("category", JoinType.RIGHT_OUTER_JOIN);
This way result set contains only Category
. 这样,结果集仅包含
Category
。 How can I add Subscriber
in Criteria
projection? 如何在“
Criteria
投影中添加Subscriber
?
I guess what you are looking for is entity graphs. 我猜您正在寻找的是实体图。
https://thoughts-on-java.org/jpa-21-entity-graph-part-1-named-entity/ https://thoughts-on-java.org/jpa-21-entity-graph-part-1-named-entity/
The link above gives an example of in jpa 2.1 but by the code you have provided it looks like you are on hibernate 4. Maybe it is time to upgrade if you have the luxury. 上面的链接给出了jpa 2.1中的示例,但是通过您提供的代码,您看起来像在休眠4上。如果您愿意,也许是时候进行升级了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.