-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Fetch attribute help needed
PostPosted: Mon Sep 15, 2008 5:58 pm 
Beginner
Beginner

Joined: Sun Oct 14, 2007 7:29 pm
Posts: 23
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>


Top
 Profile  
 
 Post subject: Re: Fetch attribute help needed
PostPosted: Wed Jun 27, 2012 7:02 am 
Newbie

Joined: Wed Jun 27, 2012 4:25 am
Posts: 1
Hey mcburton3,

Let me put it this way:

Irrespective of the combination of usage of 'lazy' and 'fetch' attributes,
1 -> We use lazy to define a contract on how association is to be fetched on load of parent object.
2 -> We use fetch to tune performance.

To be more clear on point 2:
When fetch= join --> Hibernate retrieves the associated instance or collection in the same SELECT, using an OUTER JOIN.
When fetch=select --> a second SELECT is used to retrieve the associated entity or collection. Unless you explicitly disable lazy fetching by specifying lazy="false", this second select will only be executed when you access the association.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.