ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ing. Jan Novotný <novotn...@gmail.com>
Subject Multiple selects per lazy loaded objects?!
Date Thu, 18 Feb 2010 13:43:24 GMT
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
--------------------------------------------------------------

Mime
View raw message