首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >仅为HQL查询中指定的映射获取数据

仅为HQL查询中指定的映射获取数据
EN

Stack Overflow用户
提问于 2015-06-21 10:17:05
回答 1查看 454关注 0票数 2

我有以下hbm.xml文件

代码语言:javascript
复制
    <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"

我想执行任务

代码语言:javascript
复制
from user as u
left join fetch u.settings
where u.id=25

我的问题是,当我想从用户那里获取数据时,它总是从所有关联的表( fetch="join" )中获取数据。我想知道如何只获取相关的连接数据。对于上面的查询,当我们为多个表指定了fetch="join"时,只应该从用户和设置数据中获取数据,而不是从其他表中获取数据。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-21 15:25:19

您不应该使用fetch=“联接”,因为急切的获取会导致笛卡尔积,而且效率不高。

您需要将这些关联设置为lazy="true",并且只在查询的基础上获取它们:

代码语言:javascript
复制
from user as u
left join fetch u.settings
where u.id=25

这样,您将只获取用户和他们的设置,而不加入获取其他关联。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30963423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档