This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Brian C. Stewart
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone? Anyone? Bueller?
 
Brian C. Stewart
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic