Hello,
I am still somewhat new to Hibernate and trying to undrestand the fetch attribute of associations. I don't quite understand the implications of comibining fetch = "join|select" with lazy = "false|ture" and was hoping to get some guidance. I have four scenarios below with a sample mapping file -- would someone please help me understand what is going to happen in each case when using either of the following two queries:
(a) select compdeps from Compdep as compdeps
(b) select compdeps from Compdep as compdeps left join fetch compdeps.empdeps as empdeps
Case 1 (lazy = false, fetch = join): the "empdeps" set has lazy="false" and fetch="join".
Code:
<hibernate-mapping>
<class name="com.faweb.entities.Compdep" table="COMPDEP">
<id name="compdepid" type="long">
<column name="COMPDEPID" />
<generator class="identity" />
</id>
<many-to-one name="compdata" class="com.faweb.entities.Compdata" fetch="join">
<column name="COMPANYID" not-null="true" />
</many-to-one>
<many-to-one name="usrprofiles" class="com.faweb.entities.Usrprofiles" fetch="join">
<column name="UID" />
</many-to-one>
<set name="empdeps" inverse="true" lazy="false" fetch="join" cascade="all-delete-orphan">
<key>
<column name="COMPDEPID" not-null="true" />
</key>
<one-to-many class="com.faweb.entities.Empdep" />
</set>
</class>
</hibernate-mapping>
Case 2 (lazy = true, fetch = join): the "empdeps" set has lazy="true" and fetch="join".
Code:
<hibernate-mapping>
<class name="com.faweb.entities.Compdep" table="COMPDEP">
<id name="compdepid" type="long">
<column name="COMPDEPID" />
<generator class="identity" />
</id>
<many-to-one name="compdata" class="com.faweb.entities.Compdata" fetch="join">
<column name="COMPANYID" not-null="true" />
</many-to-one>
<many-to-one name="usrprofiles" class="com.faweb.entities.Usrprofiles" fetch="join">
<column name="UID" />
</many-to-one>
<set name="empdeps" inverse="true" lazy="true" fetch="join" cascade="all-delete-orphan">
<key>
<column name="COMPDEPID" not-null="true" />
</key>
<one-to-many class="com.faweb.entities.Empdep" />
</set>
</class>
</hibernate-mapping>
Case 3 (lazy = false, fetch = select): the "empdeps" set has lazy="false" and fetch="select".
Code:
<hibernate-mapping>
<class name="com.faweb.entities.Compdep" table="COMPDEP">
<id name="compdepid" type="long">
<column name="COMPDEPID" />
<generator class="identity" />
</id>
<many-to-one name="compdata" class="com.faweb.entities.Compdata" fetch="join">
<column name="COMPANYID" not-null="true" />
</many-to-one>
<many-to-one name="usrprofiles" class="com.faweb.entities.Usrprofiles" fetch="join">
<column name="UID" />
</many-to-one>
<set name="empdeps" inverse="true" lazy="false" fetch="select" cascade="all-delete-orphan">
<key>
<column name="COMPDEPID" not-null="true" />
</key>
<one-to-many class="com.faweb.entities.Empdep" />
</set>
</class>
</hibernate-mapping>
Case 4 (lazy = true, fetch = select): the "empdeps" set has lazy="true" and fetch="select".
Code:
<hibernate-mapping>
<class name="com.faweb.entities.Compdep" table="COMPDEP">
<id name="compdepid" type="long">
<column name="COMPDEPID" />
<generator class="identity" />
</id>
<many-to-one name="compdata" class="com.faweb.entities.Compdata" fetch="join">
<column name="COMPANYID" not-null="true" />
</many-to-one>
<many-to-one name="usrprofiles" class="com.faweb.entities.Usrprofiles" fetch="join">
<column name="UID" />
</many-to-one>
<set name="empdeps" inverse="true" lazy="true" fetch="select" cascade="all-delete-orphan">
<key>
<column name="COMPDEPID" not-null="true" />
</key>
<one-to-many class="com.faweb.entities.Empdep" />
</set>
</class>
</hibernate-mapping>