Hi,
I have simple database structure, tables
a,
b with master-detail relationship
a.id->b.master_id
What I have to do is to select from
b with some criteria and then left join with
a
Straightforward approach:
Code:
Criteria c = new sess.createCriteria(b.class);
c.add(Expression...);
c.setFetchMode("b.master", FetchMode.JOIN);
c.list();
generate following sql:
Code:
SELECT * FROM b LEFT OUTER JOIN a ON b.master_id=a.id WHERE ...criteria...
Problem is that
a is small table and
b is huge (hundred of thousands records) and in this sql, first join (full, on all records from
b) is performed and then result is filtered according to criteria.
What I'd like to implement is something like this:
Code:
SELECT bb.* FROM (SELECT b.* FROM b WHERE ...criteria...) bb LEFT OUTER JOIN a ON bb.master_id = a.id
- first filtering, then join (only on filtered records).
This works fine with native SQL, I wonder if it possible to implement this using Criteria API (for dynamic criteria construction).
Thanks in advance,
Andriy