Hey,
I have got the basics of HQL down, but now I'm getting into more complex queries as I port my application over and am wondering what the best way to handle this is.
I have these two POJO's and their definitions:
Code:
<class name="ovm.central.Title" table="title">
<id name="id" column="title_pk">
<generator class="sequence">
<param name="sequence">title_seq</param>
</generator>
</id>
<many-to-one name="assetBillingState" column="asset_billing_state_fk" class="ovm.central.AssetBillingState"/>
<property name="adult" column="adult"/>
<property name="length" column="length"/>
<property name="boxOffice" column="box_office"/>
<property name="status" column="status"/>
<property name="director" column="director"/>
<property name="producer" column="producer"/>
<property name="actors" column="actors"/>
<property name="macrovision" column="macrovision"/>
<set name="titleLanguageDataInternal" table="title_lang_data">
<key column="title_fk"/>
<one-to-many class="ovm.central.TitleLanguageData"/>
</set>
</class>
<class name="ovm.central.TitleLanguageData" table="title_lang_data">
<id name="id" column="title_lang_data_pk">
<generator class="sequence">
<param name="sequence">title_lang_data_seq</param>
</generator>
</id>
<many-to-one name="title" column="title_fk" class="ovm.central.Title"/>
<many-to-one name="language" column="language_fk" class="ovm.central.Language"/>
<property name="titleString" column="title"/>
<property name="synopsisString" column="synopsis"/>
</class>
I am trying to query all titles, but I want to only show the english title string. I have this working HQL:
Code:
return sessionFactory.getCurrentSession().createQuery("SELECT title FROM Title title JOIN title.titleLanguageDataInternal tld WHERE title.id = tld.title.id AND tld.language.abbr = :abbr ORDER BY tld.title")
.setString("abbr", "en")
.list();
However, when it runs, it runs 1400 queries, basically subqueries for each title returned which it shouldn't have to. It should be a standard inner join, what am I missing?
The subquery generated looks like this:
Code:
Hibernate: select titlelangu0_.title_fk as title2_2_, titlelangu0_.title_lang_data_pk as title1_2_, titlelangu0_.title_lang_data_pk as title1_994_1_, titlelangu0_.title_fk as title2_994_1_, titlelangu0_.language_fk as language3_994_1_, titlelangu0_.title as title994_1_, titlelangu0_.synopsis as synopsis994_1_, language1_.language_pk as language1_991_0_, language1_.language as language991_0_, language1_.abbr as abbr991_0_ from title_lang_data titlelangu0_ left outer join languages language1_ on titlelangu0_.language_fk=language1_.language_pk where titlelangu0_.title_fk=?
Thanks in advance.