I have been trying to remove all SQL code from Java and some of the queries which have filtering needed, I am using enableFilter as discussed in Chapter 17 of hibernate documentation.
in DemoResponse.hbm.xml
<filter name="filterCode" condition="ASSESSMENT_CODE = :assessmentCode"></filter>
this gets picked up
in DemoRequest.hbm.xml
<filter name="filterId" condition=":fId = F_ID"/>
when using query like this
session.enableFilter("filterCode").setParameter("assessmentCode", "OK");
//this doesn't add to SQL
session.enableFilter("filterId").setParameter("fId", "1");
session.createQuery("from DemoResponse res join res.demoRequest req").list();
Note: if you plan on using filters with outer joining (either through HQL or load fetching) be careful of the direction of the condition expression. Its safest to set this up for left outer joining; in general, place the parameter first followed by the column name(s) after the operator.
The documentation has the above lines. But what I understand from that is
:fId = F_ID or F_ID = :fId makes a difference. I did try both variations.
To make matter simple, I am doing a natural join, no outer join although ultimately I have to do right outer join.
The chapter on filter is good but it is somewhat more elaborated on simple stuff but only those 2 lines on joins. Would be good if few lines of how to was there for filtering on join.
Any help appreciated.
Hibernate version: 3.2rc2
Mapping documents:
Code between sessionFactory.openSession() and session.close():
session.enableFilter("filterId").setParameter("fid", "TST");
session.enableFilter("filterCode").setParameter("assessmentCode", "R");
//this works
List list = session.createQuery("from DemoResponse res, DemoRequest req where res.id.tId = req.id.tId and res.id.sNumber = req.id.sNumber").list();
//this doesn't
List list = session.createQuery("from DemoResponse res join res.demoRequest req").list();
Full stack trace of any exception that occurs:
none.. when doing join the filters from one side are not getting added to the SQL code by SQLGenerator
Name and version of the database you are using:
Oracle 9
The generated SQL (show_sql=true):
DEMO_RESPONSE demores0_ inner join DEMO_REQUEST demoreq1_ on demores0_.T_ID=demoreq1_.T_ID and demores0_.S_NUMBER=demoreq1_.S_NUMBER where demores0_.ASSESSMENT_CODE = :filterCode.assessmentCode
select demores0_.T_ID as T1_2_0_, demores0_.S_NUMBER as SEQUENCE2_2_0_, from DEMO_RESPONSE demores0_ inner join DEMO_REQUEST demoreq1_ on demores0_.T_ID=demoreq1_.T_ID and demores0_.S_NUMBER=demoreq1_.S_NUMBER where demores0_.ASSESSMENT_CODE = ?
Debug level Hibernate log excerpt:
|