I have following hbm.xml
file
<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>
..
..
...
and many tables associate with user here I am using fetch="join"
for maximum tables.
In other queries I have to fetch data from few other related tables so I made
fetch="join"
.
I want to execute quest
from user as u
left join fetch u.settings
where u.id=25
My problem is when I want to fetch data from user it always fetch data from all associated table where fetch="join"
.
I want to know how to fetch only related join data. For above query only data from user and settings data should be fetched not from other tables when we have specified fetch="join"
for multiple tables.
You should not use fetch="join" because EAGER fetching can lead to Cartesian Products and it's not very efficient.
You need to set those associations to
lazy="true"
and only fetch themon a query basis:This way, you will only fetch Users along with their settings and without join fetching the other associations.