简体   繁体   中英

Trouble converting SQL query with subselect to Hibernate named query

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?

Have you tried using an inner join; something like the following SQL:

select A.*,distinct B.FZGLP_CODE, B.FZGLP_BASE 
from TFZGLP B
inner join TIBP A on A.IBP_CODE = B.FZGLP_CODE
    where A.IBP_BASE = B.FZGLP_BASE

You can easily convert this to hibernate.

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