我有以下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
我的问题是,当我想从用户那里获取数据时,它总是从所有关联表where中获取数据fetch="join"
。我想知道如何仅获取相关的联接数据。对于上面的查询,当我们fetch="join"
为多个表指定了数据时,仅应从用户和设置数据中获取数据,而不能从其他表中获取数据。
您不应该使用fetch =“ join”,因为EAGER的提取会导致笛卡尔积,而且效率不是很高。
您需要将这些关联设置为,lazy="true"
并且仅在查询基础上获取它们:
from user as u
left join fetch u.settings
where u.id=25
这样,您将只获取用户及其设置,而无需加入获取其他关联。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句