Announcement Announcement Module
Collapse
No announcement yet.
Spring + iBAtis 2.0 Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + iBAtis 2.0

    I am trying to use iBatis 2.0.8 with SpringFramework 1.1.3. I created an ApplicationContext file as follow:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>   
    	<!-- define a jdbc property file for CRS 3.1 -->
    	
    	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    		<property name="location"><value>jdbc.properties</value></property>
    	</bean>
    	
    	
    	<!-- crs31 Data source -->
    	
    	<bean id="crs31DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">			
    		<property name="driverClassName"><value>$&#123;crs31.jdbc.driverClassName&#125;</value></property>
    		<property name="url"><value>$&#123;crs31.jdbc.url&#125;</value></property>
    		<property name="username"><value>$&#123;crs31.jdbc.username&#125;</value></property>
    		<property name="password"><value>$&#123;crs31.jdbc.password&#125;</value></property>
    	</bean>
    
        <!-- itemDAO implementation in iBatis -->
    	
    	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
    		<property name="dataSource"><ref bean="crs31DataSource"></ref></property>
    		<property name="configLocation"><value>sql-maps-config.xml</value></property>
    	</bean>
    	
        <bean id="ItemDAO" class="com.newgenesys.crs.dao.ibatis.ItemDaoImplementation">
    		<property name="dataSource"><ref bean="crs31DataSource"></ref></property>
    		<property name="sqlMapClient"><ref local="sqlMapClient"/></property>
        </bean>
    </beans>
    Then I have a sql-maps-config.xml defined as follow:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sqlMapConfig PUBLIC 
    	"-//iBATIS.com/DTD SQL Map Config 2.0//EN"
    	"http&#58;//www.ibatis.com/dtd/sql-map-config-2.dtd">
    
    <sqlMapConfig>
    	<settings enhancementEnabled="true" maxTransactions="5"
    		maxRequests="32" maxSessions="10" />
    
    	<sqlMap resource="com/newgenesys/crs/dao/ibatis/maps/Item.xml" />
    	<sqlMap resource="com/newgenesys/crs/dao/ibatis/maps/ProductCode.xml" />
    
    
    </sqlMapConfig>
    And an item.xml defined as follow:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sql-map
        PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
        "http&#58;//www.ibatis.com/dtd/sql-map.dtd">
    
    <sql-map name="Item">
    
    	<!--  General -->
    	
    	<cache-model name="item-cache" implementation="LRU"
    		reference-type="WEAK">
    		<flush-interval hours="24" />
    		<cache-property name="size" value="1000" />
    	</cache-model>
    
    	<!-- Results definition -->
    
    	<result-map name="map-item"
    		class="com.newgenesys.crs.core.pojo.Item">
    		<property name="SKU" />
    		<property name="Description" />
    		<property name="ProductCode.code" column="ProductCode" />
    	</result-map>
    
    	<!-- singleton Select statements -->
    
    	<mapped-statement name="getSize" result-class="int">
    		select count&#40;*&#41; from items
    	</mapped-statement>
    
    	<mapped-statement name="getItem" result-map="map-item">
    		select * from Items where SKU = #value#
    	</mapped-statement>
    
    	<!-- List select statements -->
    
    	<mapped-statement name="getItemList" result-map="map-item">
    		select * from Items
    	</mapped-statement>
    
    	<mapped-statement name="getItemListByProduct" result-map="map-item">
    		select SKU, Description, ProductCode from Items 
    		where Items.ProductCode = #value#
    	</mapped-statement>
    
    
    </sql-map>
    My class implementation is a follow:
    Code:
    /*
     * Created on Dec 15, 2004
     *
     */
    
    package com.newgenesys.crs.dao.ibatis;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.springframework.dao.DataAccessException;
    import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
    
    import com.newgenesys.crs.core.pojo.Item;
    import com.newgenesys.crs.core.pojo.ProductCode;
    import com.newgenesys.crs.core.pojo.Vendor;
    import com.newgenesys.crs.dao.ItemDao;
    
    public class ItemDaoImplementation extends SqlMapClientDaoSupport implements ItemDao &#123;
    
        public Item getItem&#40;String aSKU&#41; throws DataAccessException &#123;
            return &#40;Item&#41; getSqlMapClientTemplate&#40;&#41;.queryForObject&#40;"getItemWithSKU", aSKU&#41;;
        &#125;
    
        public List getItemListByProductCode&#40;ProductCode aCode&#41; throws DataAccessException &#123;
            return getSqlMapClientTemplate&#40;&#41;.queryForList&#40;"getItemListForProductCode", aCode.getCode&#40;&#41;&#41;;
        &#125;
    
        public List getItemListByVendor&#40;Vendor aVendor&#41; throws DataAccessException &#123;
            return getSqlMapClientTemplate&#40;&#41;.queryForList&#40;"getItemListForVendor", aVendor.getID&#40;&#41;&#41;;
        &#125;
    
        public void insertItem&#40;Item anItem&#41; throws DataAccessException &#123;
        &#125;
    
        public void updateItem&#40;Item anItem&#41; throws DataAccessException &#123;
        &#125;
    
        public List getItemList&#40;&#41; throws DataAccessException &#123;
            return getSqlMapClientTemplate&#40;&#41;.queryForList&#40;"getItemList", null&#41;;
        &#125;
    
    &#125;
    The problem I am having is that when I try to access getItemList I get an error message telling me that there is no map called getItemList.

    Here is the call:

    Code:
            ApplicationContext ctx = new ClassPathXmlApplicationContext&#40;paths&#41;;
    
            DataSource ds = &#40;DataSource&#41; ctx.getBean&#40;"crs31DataSource"&#41;;
            ItemDao dao = &#40;ItemDao&#41; ctx.getBean&#40;"ItemDAO"&#41;;
    
            List items = dao.getItemList&#40;&#41;;
    and the trace:

    Code:
    Exception in thread "main" com.ibatis.sqlmap.client.SqlMapException&#58; There is no statement named getItemList in this SqlMap.
    	at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.getMappedStatement&#40;SqlMapExecutorDelegate.java&#58;287&#41;
    	at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList&#40;SqlMapExecutorDelegate.java&#58;617&#41;
    	at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForList&#40;SqlMapExecutorDelegate.java&#58;598&#41;
    	at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForList&#40;SqlMapSessionImpl.java&#58;107&#41;
    	at org.springframework.orm.ibatis.SqlMapClientTemplate$3.doInSqlMapClient&#40;SqlMapClientTemplate.java&#58;202&#41;
    	at org.springframework.orm.ibatis.SqlMapClientTemplate.execute&#40;SqlMapClientTemplate.java&#58;142&#41;
    	at org.springframework.orm.ibatis.SqlMapClientTemplate.executeWithListResult&#40;SqlMapClientTemplate.java&#58;164&#41;
    	at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList&#40;SqlMapClientTemplate.java&#58;200&#41;
    	at com.newgenesys.crs.dao.ibatis.ItemDaoImplementation.getItemList&#40;ItemDaoImplementation.java&#58;40&#41;
    	at com.newgenesys.crs.dao.test.ItemDaoTest2.main&#40;ItemDaoTest2.java&#58;38&#41;
    After running through the debugger, I can see that while the data source of the SqlMapClient is loaded, all the maps are not. It seems that specifying the configLocation is not enough to load the mapped-statements.

    Could you help me solve that problem

    Thanks in advance

    Pascal

    PS: Sorry for the long post, I thought putting as much info upfront would be best

  • #2
    Spring + iBAtis 2.0 (Possible bug)

    Working with the debugger I found a possible bug in XmlSqlMapClientBuilder. According to the DTD used by Ibatis 2.0.8 the statement of a sqlMap is now called mapped-statement. However the method parseSqlMap of XmlSqlMapCkientBuilder still looks for a statement node. I am not sure if this is a problem with ibatis or Spring!

    As anybody tried spring with ibatis 2.0. Maybe I am missing something in my config files!

    Pascal

    Comment


    • #3
      Give iBatis 1.0 a try (http://sourceforge.net/project/showf...group_id=61326). I have also noticed that Spring seems to be using the iBatis 1.x API, e.g. in the JPetStore sample application.

      Erwin

      Comment


      • #4
        Hi Pascal,

        I'm using SqlMaps 2.0.8 without any problem. One thing I noticed in your code is that you are using the SqlMaps 1.x XML tags in your maps. That syntax should be compatible with the 2.x versions, but you might be better off switching to the 2.x tags. See the reference guide in the iBatis distribution for more details.

        The other thing is that you should probably be using <select>, <insert>, <update> and <delete> instead of the generic <statement>.

        Also, how do you know that the SqlMapClientFactoryBean is actually loading your file? You're using a classpath loader - perhaps it is a classpath issue?

        Geoff

        Comment


        • #5
          Geoff,

          You are right. I missed changing those maps to 2.0 and was still using the old model. Thanks for pointing that out. Now it works as expected.

          Thanks

          Pascal

          Comment


          • #6
            I had the exact same problem as pdemilly. I'll try changing the SQL maps.

            Comment

            Working...
            X