简体   繁体   中英

Cast to subclass when joining with the base class using CriteriaBuilder (OpenJPA)

I have a simple hierarchy TradeCustomer is an OrganisationRole . An OrganisationRole is linked to Organisation s. Organisation has a property roles containing all roles of an organisation.

I'd like to write a JPA query using the CriteriaBuilder which root is Organisation but access a property of TradeCustomer , eg, I need to join Organisation with OrganisationRole and cast it to TradeCustomer .

Here is my solution:

CriteriaQuery<String> query = cb.createQuery(String.class);
Root<Organisation> root = query.from(Organisation.class);
Join<Organisation, TradeCustomer> tcJoin = root.join("roles");
query.select(tcJoin.get(TradeCustomer_.rate));

The important bit is to use the name of the column ( roles ) instead of Organisation_.roles.

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