簡體   English   中英

僅獲取 HQL 查詢中指定映射的數據

[英]Fetching data only for specified mapping in HQL query

我有以下hbm.xml文件

    <hibernate-mapping>
      <class catalog="test_user" name="test.user" table="user">
        <id name="id" type="java.lang.Long">
          <column name="id"/>
          <generator class="identity"/>
        </id>
        <property name="name" type="string">
          <column length="200" name="name" unique="true"/>
        </property>

    <set fetch="join" inverse="true" lazy="true" name="education" table="user_education">
          <key>
            <column name="aid"/>
          </key>
          <one-to-many class="test.UserEducation"/>
        </set>
<set fetch="join" inverse="true" lazy="true" name="employment" table="user_employment">
          <key>
            <column name="aid"/>
          </key>
          <one-to-many class="test.UserEmployment"/>
        </set>
<set fetch="join" inverse="true" lazy="false" name="otherProfiles" table="user_other_profile">
          <key>
            <column name="aid"/>
          </key>
          <one-to-many class="test.OtherProfile"/>
        </set>
<set fetch="join" inverse="true" lazy="false" name="settings" table="user_setting">
          <key>
            <column name="aid"/>
          </key>
          <one-to-many class="test.Setting"/>
        </set>
<set fetch="join" inverse="true" lazy="false" name="images" table="user_images">
          <key>
            <column name="aid"/>
          </key>
          <one-to-many class="test.Images"/>
        </set>
 ..
..
...

並且這里有許多表與用戶相關聯,我使用fetch="join"fetch="join"最大表。 在其他查詢中,我必須從其他幾個相關表中獲取數據,所以我做了fetch="join"

我要執行任務

from user as u
left join fetch u.settings
where u.id=25

我的問題是當我想從用戶那里獲取數據時,它總是從所有關聯的表中fetch="join"數據,其中fetch="join" 我想知道如何只獲取相關的連接數據。 對於上述查詢,當我們為多個表指定fetch="join"時,不應從其他表中獲取用戶和設置數據的數據。

你不應該使用 fetch="join" 因為EAGER fetching 會導致笛卡爾積,而且效率不高

您需要將這些關聯設置為lazy="true"並且只獲取查詢基礎:

from user as u
left join fetch u.settings
where u.id=25

這樣,您將只獲取用戶及其設置,而不會加入獲取其他關聯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM