I have two tables and I want to select entries from table A who are referenced in table B using two fields present in both tables.
The two fields used to reference the entries are numeric, so the following query does what I want (but it is slow due to the multiplication and because table B is large).
select * from TIBP
where IBP_CODE * 100 + IBP_BASE in
(select FZGLP_CODE * 100 + FZGLP_BASE from TFZGLP)
I've successfully mapped this to the following HQL named query:
<query name="findWhereReferencedInZglp">
select tibp
from TibpEntity as tibp
where tibp.code * 100 + tibp.base in
(select zglp.code * 100 + zglp.base from ZglpEntity as zglp)
</query>
To improve the speed of the above, I've modified the SQL statement as follows:
select * from TIBP as A,
(select distinct FZGLP_CODE, FZGLP_BASE from TFZGLP) as B
where A.IBP_CODE = B.FZGLP_CODE and A.IBP_BASE = B.FZGLP_BASE
Sadly, I've not managed to turn this into a named HQL query that can be parsed. Is this even possible using HQL/Hibernate?