This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Hibernate doing a JOIN and a SELECT though only a JOIN is specified as FetchType and on the hbm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate doing a JOIN and a SELECT though only a JOIN is specified as FetchType and on the hbm" Watch "Hibernate doing a JOIN and a SELECT though only a JOIN is specified as FetchType and on the hbm" New topic
Author

Hibernate doing a JOIN and a SELECT though only a JOIN is specified as FetchType and on the hbm

Brian C. Stewart
Greenhorn

Joined: Jun 10, 2004
Posts: 4
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 :-)
Brian C. Stewart
Greenhorn

Joined: Jun 10, 2004
Posts: 4
Anyone? Anyone? Bueller?
Brian C. Stewart
Greenhorn

Joined: Jun 10, 2004
Posts: 4
In case anybody looks at this in the future, after much reading and experimentation, I've come up with the following criteria query to fix my issue. Bascially specifying the join type as part of creating the Criteria is key. Also, this won't work if the key-many-to-one is used because it doesn't properly handle joins. I'm not really sure if I need the result transformer here. It didn't affect the results, so I commented it out.

 
GeeCON Prague 2014
 
subject: Hibernate doing a JOIN and a SELECT though only a JOIN is specified as FetchType and on the hbm