Yes,
Code:
from Product prod where 0 < ( select count(*) from prod.parts part where part.psize=5 and part.cost<10 )
does work and solves my problem.
However, the generated SQL is not really efficient. The count prevents an early exit, it would be desireable to use a query similar to
Code:
from Product prod where exists ( select 1 from prod.parts part where part.psize=5 and part.cost<10 )
To get really good performance from subqueries we need to be able to generate SQL of two types:
Code:
SELECT * FROM Table1 WHERE EXISTS (SELECT * FROM Table2 WHERE Table1.column1 = Table2.column1)
This would execute with a nested
out-to-in loop and is preferable when Table1 is smaller or the query has additional restrictions on Table1.
Code:
SELECT * FROM Table1 WHERE Table1.column1 IN (SELECT Table2.column1 FROM Table2)
This would execute with a nested
in-to-out loop and is preferable when Table2 is smaller or the query has additional restrictions on Table2.
Is it possible to get Hibernate to generate these types of subqueries? Hibernate3?
Peter