posted 12 years ago
I have a class/table that contains a relationship to another class/table which then contains a collection of the original class/table. It goes something like this. An Organization has an ExamCode. That ExamCode will also belong to other Organizations. Since I'm creating a view that needs to show this relationship, I need to pull it in a single query (ideally).
I have the hbm files configured correctly I believe (code posted below). I'm calling a createCriteria query to get the data. It almost works. The first query generated (posted) has everything that I need. It even puts in a date restriction that is necessary toward the bottom of the sql generated. Then, unfortunately, Hibernate generates hundreds of select statements that are completely un-needed, and infact are not restricting the date, therefore replacing the objects (collection) already created by the nice join with incorrect data.
Some things that I've tried include putting fetch="join" and lazy="false" in the hbm files, Adding FetchModes to the criteria, and playing with the Inverse indicator. I also discovered to my delight that createAlias works like createCriteria inside of another criteria. This is good news, because before that, the 'inner' createCriteria were not applying the FetchModes and thus they had to be hardcoded in the hbm (ouch).
Does anybody know how I can correctly instruct Hibernate to stop generating these extra selects?
ExamCode.hbm.xml
...
...
OrgExamCode.hbm.xml
<hibernate-mapping>
...
...
OrgExamCodeDAO.java
Generated SQL JOIN (good stuff)
Extra un-needed stuff that also gets generated, one for each ExamCode (hundreds)
Thanks for your help :-)