lazy-loading

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

lazy-loading

Vogon
Hi guys,

How does lazy-loading work in MyBatis?

For example :


public static void main(String args[]){

        SqlSession session = iBATISUtilsXML.getSession();

        List<Bid>bids = (List<Bid>)session.selectList("getAllBids");

        for(Bid bid : bids){
           System.out.println("Identifier : "+bid.getId());
           System.out.println("Amount     :"+bid.getAmount());
        }

        session.close();

        }





with this mapping:



        <resultMap id="bidMapper" type="Bid">

             <id     property="id"         column="BID_ID"/>

             <result property="amount"     column="amount"/>

             <result property="created"    column="created"/>

             <association property="item"  column="ITEM_ID"
javaType="Item" select="readItem"/>

        </resultMap>



    <select id="getAllBids" resultMap="bidMapper">

       select * from BID

    </select>




with any combination of   <setting name="aggressiveLazyLoading"
value="true"/>
and <setting name="lazyLoadingEnabled" value="true"/>  in my
Configuration.xml

iBATIS ALWAYS performs an extra-select to retrieve Item object :

    DEBUG PreparedStatement:28 - ==>  Executing: select * from ITEM
where ITEM_ID=?

I don't want this extra-select, because  I didn't access the property
"item"  in any way.

How can I do ?

Reply | Threaded
Open this post in threaded view
|

Re: lazy-loading

Raj Nagappan
Hi Vogon,

I came across this myself a few weeks ago. The default behaviour in
Mybatis 3 is to aggressively lazy load all properties in an object as
soon as any property in that object is accessed (even if not the lazy
loaded property). You be able to defer lazy loading as long as
possible by:
1. <setting name="aggressiveLazyLoading" value="false"/> and
2. using the cglib-nodep jar if you want to lazy load concrete classes
(as opposed to only interfaces).

Just a caution if you have a class A and lazy load a property B into
it, then anywhere inside A you have to call getB() rather than B
directly to ensure that the lazy loader runs on it.

Raj.

On Oct 7, 1:27 am, Vogon <[hidden email]> wrote:

> Hi guys,
>
> How does lazy-loading work in MyBatis?
>
> For example :
>
> public static void main(String args[]){
>
>         SqlSession session = iBATISUtilsXML.getSession();
>
>         List<Bid>bids = (List<Bid>)session.selectList("getAllBids");
>
>         for(Bid bid : bids){
>            System.out.println("Identifier : "+bid.getId());
>            System.out.println("Amount     :"+bid.getAmount());
>         }
>
>         session.close();
>
>         }
>
> with this mapping:
>
>         <resultMap id="bidMapper" type="Bid">
>
>              <id     property="id"         column="BID_ID"/>
>
>              <result property="amount"     column="amount"/>
>
>              <result property="created"    column="created"/>
>
>              <association property="item"  column="ITEM_ID"
> javaType="Item" select="readItem"/>
>
>         </resultMap>
>
>     <select id="getAllBids" resultMap="bidMapper">
>
>        select * from BID
>
>     </select>
>
> with any combination of   <setting name="aggressiveLazyLoading"
> value="true"/>
> and <setting name="lazyLoadingEnabled" value="true"/>  in my
> Configuration.xml
>
> iBATIS ALWAYS performs an extra-select to retrieve Item object :
>
>     DEBUG PreparedStatement:28 - ==>  Executing: select * from ITEM
> where ITEM_ID=?
>
> I don't want this extra-select, because  I didn't access the property
> "item"  in any way.
>
> How can I do ?
Reply | Threaded
Open this post in threaded view
|

Re: lazy-loading

Vogon
Thanks Raj !!

On 7 Ott, 00:20, Raj Nagappan <[hidden email]> wrote:

> Hi Vogon,
>
> I came across this myself a few weeks ago. The default behaviour in
> Mybatis 3 is to aggressively lazy load all properties in an object as
> soon as any property in that object is accessed (even if not the lazy
> loaded property). You be able to defer lazy loading as long as
> possible by:
> 1. <setting name="aggressiveLazyLoading" value="false"/> and
> 2. using the cglib-nodep jar if you want to lazy load concrete classes
> (as opposed to only interfaces).
>
> Just a caution if you have a class A and lazy load a property B into
> it, then anywhere inside A you have to call getB() rather than B
> directly to ensure that the lazy loader runs on it.
>
> Raj.
>
> On Oct 7, 1:27 am, Vogon <[hidden email]> wrote:
>
> > Hi guys,
>
> > How does lazy-loading work in MyBatis?
>
> > For example :
>
> > public static void main(String args[]){
>
> >         SqlSession session = iBATISUtilsXML.getSession();
>
> >         List<Bid>bids = (List<Bid>)session.selectList("getAllBids");
>
> >         for(Bid bid : bids){
> >            System.out.println("Identifier : "+bid.getId());
> >            System.out.println("Amount     :"+bid.getAmount());
> >         }
>
> >         session.close();
>
> >         }
>
> > with this mapping:
>
> >         <resultMap id="bidMapper" type="Bid">
>
> >              <id     property="id"         column="BID_ID"/>
>
> >              <result property="amount"     column="amount"/>
>
> >              <result property="created"    column="created"/>
>
> >              <association property="item"  column="ITEM_ID"
> > javaType="Item" select="readItem"/>
>
> >         </resultMap>
>
> >     <select id="getAllBids" resultMap="bidMapper">
>
> >        select * from BID
>
> >     </select>
>
> > with any combination of   <setting name="aggressiveLazyLoading"
> > value="true"/>
> > and <setting name="lazyLoadingEnabled" value="true"/>  in my
> > Configuration.xml
>
> > iBATIS ALWAYS performs an extra-select to retrieve Item object :
>
> >     DEBUG PreparedStatement:28 - ==>  Executing: select * from ITEM
> > where ITEM_ID=?
>
> > I don't want this extra-select, because  I didn't access the property
> > "item"  in any way.
>
> > How can I do ?