Thank you for answer. I understand now why all data were loaded at the first
touch to the object and not at time when particular property is needed. But
I still don't undestand why iBatis make two same select queries to the
database (see the log please) - this might hit a performance in some cases.
I still don't see the point.
Thanks.

Jan

2010/2/18 Clinton Begin <clinton.be...@gmail.com>

> This was a topic of much debate.  It does that to allow internal references
> to private fields.  But some people don't like that behavior, so in the next
> release (or in trunk if you want to check it out and build it), there's a
> configuration switch.  The docs are already updated, so look for the
> aggressiveLazyLoad setting.
>
> Clinton
>
> 2010/2/18 Ing. Jan Novotný <novotn...@gmail.com>
>
>> Hello again,
>>
>>
>>    I've found that for lazily loaded objects triggers multiple unnecessary
>> selects and are executed all at load time not at the time, when I touch the
>> property. I use 3-beta-9 and my settings are following:
>>
>> <settings>
>>         <setting name="cacheEnabled" value="true"/>
>>         <setting name="lazyLoadingEnabled" value="true"/>
>>         <setting name="multipleResultSetsEnabled" value="true"/>
>>         <setting name="useColumnLabel" value="true"/>
>>         <setting name="useGeneratedKeys" value="false"/>
>>     </settings>
>>
>> <resultMap id="thinProductLazyLoaded" type="product">
>>         <id property="id" column="id"/>
>>         <result property="name" column="name"/>
>>         <association property="group" column="idGroup" javaType="group"
>>
>> select="cz.novoj.ibatis.ProductGroupMapper.getGroupById"/>
>>     </resultMap>
>>
>> <select id="getLazyProductById" parameterType="int"
>> resultMap="thinProductLazyLoaded">
>>         select * from product where id = #{id}
>>     </select>
>>
>> <select id="getGroupById" parameterType="int" resultType="group">
>>         select * from productGroup where id = #{id}
>>     </select>
>>
>>    Test:
>>
>> @Test
>>     public void testGetLazyProductById() throws Exception {
>>         Product product = productMapper.getLazyProductById(1);
>>         assertNotNull(product);
>>         System.out.println("#1");
>>         assertEquals(1, (int)product.getId());
>>         System.out.println("#2");
>>         assertEquals("Lenovo ThinkCentre 250GB Serial ATA Hard Disk
>> Drive", product.getName());
>>         assertNotNull(product.getGroup());
>>         System.out.println("#3");
>>         assertEquals("HDD", product.getGroup().getName());
>>         assertEquals("HARDWARE", product.getGroup().getGroupType());
>>         assertNull(product.getTags());
>>     }
>>
>>    Output:
>>
>> DEBUG [main][2010-02-18 14:42:34,140][java.sql.Connection]: ooo Connection
>> Opened
>> DEBUG [main][2010-02-18 14:42:34,234][java.sql.PreparedStatement]: ==>
>> Executing: select * from product where id = ?
>> DEBUG [main][2010-02-18 14:42:34,234][java.sql.PreparedStatement]: ==>
>> Parameters: 1(Integer)
>> DEBUG [main][2010-02-18 14:42:34,250][java.sql.ResultSet]: <==    Columns:
>> ID, NAME, IDGROUP
>> DEBUG [main][2010-02-18 14:42:34,250][java.sql.ResultSet]: <==        Row:
>> 1, Lenovo ThinkCentre 250GB Serial ATA Hard Disk Drive, 1
>> DEBUG [main][2010-02-18 14:42:34,359][java.sql.Connection]: xxx Connection
>> Closed
>> #1
>> DEBUG [main][2010-02-18 14:42:34,375][java.sql.Connection]: ooo Connection
>> Opened
>> DEBUG [main][2010-02-18 14:42:34,375][java.sql.PreparedStatement]: ==>
>> Executing: select * from productGroup where id = ?
>> DEBUG [main][2010-02-18 14:42:34,375][java.sql.PreparedStatement]: ==>
>> Parameters: 1(Integer)
>> DEBUG [main][2010-02-18 14:42:34,375][java.sql.ResultSet]: <==    Columns:
>> ID, NAME, GROUPTYPE
>> DEBUG [main][2010-02-18 14:42:34,375][java.sql.ResultSet]: <==        Row:
>> 1, HDD, HARDWARE
>> DEBUG [main][2010-02-18 14:42:34,390][java.sql.Connection]: xxx Connection
>> Closed
>> DEBUG [main][2010-02-18 14:42:34,390][java.sql.Connection]: ooo Connection
>> Opened
>> DEBUG [main][2010-02-18 14:42:34,390][java.sql.PreparedStatement]: ==>
>> Executing: select * from productGroup where id = ?
>> DEBUG [main][2010-02-18 14:42:34,390][java.sql.PreparedStatement]: ==>
>> Parameters: 1(Integer)
>> DEBUG [main][2010-02-18 14:42:34,390][java.sql.ResultSet]: <==    Columns:
>> ID, NAME, GROUPTYPE
>> DEBUG [main][2010-02-18 14:42:34,390][java.sql.ResultSet]: <==        Row:
>> 1, HDD, HARDWARE
>> DEBUG [main][2010-02-18 14:42:34,390][java.sql.Connection]: xxx Connection
>> Closed
>> #2
>> #3
>>
>>    Could someone explain how lazy really loading works? As I see, the lazy
>> loading paradigm has changed a lot since iBatis 2.x
>>
>> Jan
>>
>> --
>> --------------------------------------------------------------
>> Ing. Jan Novotný
>> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>> http://blog.novoj.net
>> Myšlenky dne otce Fura
>> --------------------------------------------------------------
>>
>
>


-- 
--------------------------------------------------------------
Ing. Jan Novotný
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
http://blog.novoj.net
Myšlenky dne otce Fura
--------------------------------------------------------------

Reply via email to