简体   繁体   中英

Criteria query for outer join to subquery

I have two tables, tableA and tableB:

tableA: int id, String val
tableB: int id, String logs, int a_ref.

I need to create a query such as:

SELECT tabB.* FROM tableB tabB
LEFT OUTER JOIN tableA tabA ON tabA.id = tabB.a_ref;

But I can't change the metadata (modify the Hibernate mapping files ( .hbm ) for the respective tables).

How to create a criteria query in hibernate for this?

Thanks in advance.

A HQL or Criteria query is based on entities and relationships between entities, and you didn't show us you entities, so we can only speculate. If TABLEA is mapped to TableA and TABLEB is mapped to TableB , with a many-to-one association between TableB and TableA, then the query is obvious:

select b from TableB b left join b.tableA

But note that the left join is completely useless here, since it doesn't add any restriction to the query, and the query doesn't select anything from TABLEA . Your initial SQL query should very well be written as select tabB.* from TABLEB tabB . And the criteria query would thus just be

Criteria c = session.createCriteria(TableB.class)

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