我有以下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"。
我想执行任务
from user as u
left join fetch u.settings
where u.id=25我的问题是,当我想从用户那里获取数据时,它总是从所有关联的表( fetch="join" )中获取数据。我想知道如何只获取相关的连接数据。对于上面的查询,当我们为多个表指定了fetch="join"时,只应该从用户和设置数据中获取数据,而不是从其他表中获取数据。
发布于 2015-06-21 15:25:19
您不应该使用fetch=“联接”,因为急切的获取会导致笛卡尔积,而且效率不高。。
您需要将这些关联设置为lazy="true",并且只在查询的基础上获取它们:
from user as u
left join fetch u.settings
where u.id=25这样,您将只获取用户和他们的设置,而不加入获取其他关联。
https://stackoverflow.com/questions/30963423
复制相似问题