• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone? Anyone? Bueller?
 
Brian C. Stewart
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic