简体   繁体   中英

Hibernate Criteria - multiple entity types in projection

I have two entities Category , Subscriber . Subscriber has reference on Category (there can be only one subscriber for given category ie 1-to-1). 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 ).

I would like to rewrite this to Criteria . Since I can navigate only from Subscriber to Category I have to use RIGHT JOIN :

 Criteria c = getSession()
            .createCriteria(Subscriber.class, subscriberAlias)
            .createAlias("category", JoinType.RIGHT_OUTER_JOIN);

This way result set contains only Category . How can I add Subscriber in Criteria projection?

I guess what you are looking for is entity graphs.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM