Announcement Announcement Module
Collapse
No announcement yet.
Problem using a SqlMapClientFactoryBean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Problem using a SqlMapClientFactoryBean

    Hello,

    I'm trying use a SqlMapClientFactoryBean to create SqlMapClients. My code looks like this:

    <bean name="sqlMapClientFactoryBeanSimatel"
    class="org.springframework.orm.ibatis.SqlMapClient FactoryBean"
    singleton="true">
    <property name="configLocation">
    <value>WEB-INF/ibatisconf/SqlMapConfig-Simatel.xml</value>
    </property>
    <property name="sqlMapClientProperties">
    <props>
    <prop key="driver">org.postgresql.Driver</prop>
    <prop key="url">jdbcostgresql://localhost:5432/SIMATEL</prop>
    <prop key="username">dek</prop>
    <prop key="password">dek</prop>
    </props>
    </property>
    </bean>

    <bean id="sqlMapClientSimatel" factory-bean="sqlMapClientFactoryBeanSimatel"
    factory-method="getObject"/>

    <bean id="fachadaSimatel" class="com.yac.persistence.FachadaSimatel">
    <property name="sqlMapClient">
    <ref bean="sqlMapClientSimatel"/>
    </property>

    <property name="fachadaExterno2">
    <ref bean="fachadaExterno2"/>
    </property>
    </bean>

    But I get the error:

    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'fachadaSimatel' defined in resource [/WEB-INF/simatel-servlet.xml] of ServletContext: Can't resolve reference to bean 'sqlMapClientSimatel' while setting property 'sqlMapClient'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'sqlMapClientSimatel' defined in resource [/WEB-INF/simatel-servlet.xml] of ServletContext: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionSt oreException: Cannot find matching factory method 'getObject' on class class com.ibatis.sqlmap.engine.impl.SqlMapClientImpl

    Well, I think that is a bug.

    Help me, please.

  • #2
    I had problems with the petstoredemo and iBatis with Websphere 5.1 because WAS shippes with its own jdom lib. So I refactored the demo to use iBatis2.0 whitch does not need jdom. I haven't had any problems so here is my dataAccessContext:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <!--
    - Application context definition for JPetStore's data access layer.
    - Accessed by business layer objects defined in "applicationContext.xml"
    - (see web.xml's "contextConfigLocation").
    -
    - This version of the data access layer works on a combined database,
    - using a local DataSource with DataSourceTransactionManager. It does not
    - need any JTA support in the container: It will run as-is in plain Tomcat.
    -->
    <beans>

    <!-- ========================= RESOURCE DEFINITIONS ========================= -->

    <!-- Local DataSource that refers to a combined database -->
    <!-- (see dataAccessContext-jta.xml for an alternative) -->
    <!-- The placeholders are resolved from jdbc.properties through -->
    <!-- the PropertyPlaceholderConfigurer in applicationContext.xml -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName"><value>${jdbc.driverClassNa me}</value></property>
    <property name="url"><value>${jdbc.url}</value></property>
    <property name="username"><value>${jdbc.username}</value></property>
    <property name="password"><value>${jdbc.password}</value></property>
    </bean>

    <!-- Transaction manager for a single JDBC DataSource -->
    <!-- (see dataAccessContext-jta.xml for an alternative) -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSou rceTransactionManager">
    <property name="dataSource"><ref local="dataSource"/></property>
    </bean>

    <!-- SqlMap setup for iBATIS Database Layer -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClient FactoryBean">
    <property name="configLocation"><value>WEB-INF/sql-map-config.xml</value></property>
    </bean>


    <!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->

    <bean id="accountDao" class="org.springframework.samples.jpetstore.dao.i batis.SqlMapAccountDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMapClient"><ref local="sqlMapClient"/></property>
    </bean>

    <bean id="categoryDao" class="org.springframework.samples.jpetstore.dao.i batis.SqlMapCategoryDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMapClient"><ref local="sqlMapClient"/></property>
    </bean>

    <bean id="productDao" class="org.springframework.samples.jpetstore.dao.i batis.SqlMapProductDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMapClient"><ref local="sqlMapClient"/></property>
    </bean>

    <bean id="itemDao" class="org.springframework.samples.jpetstore.dao.i batis.SqlMapItemDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMapClient"><ref local="sqlMapClient"/></property>
    </bean>

    <!-- Refers to the combined database here -->
    <!-- (see dataAccessContext-jta.xml for an alternative) -->
    <bean id="orderDao" class="org.springframework.samples.jpetstore.dao.i batis.SqlMapOrderDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMapClient"><ref local="sqlMapClient"/></property>
    <property name="sequenceDao"><ref local="sequenceDao"/></property>
    </bean>

    <!-- OrderDao definition for MS SQL Server -->
    <!-- (to be used instead of the default orderDao) -->
    <!--
    <bean id="orderDao" class="org.springframework.samples.jpetstore.dao.i batis.MsSqlOrderDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMap"><ref local="sqlMap"/></property>
    <property name="sequenceDao"><ref local="sequenceDao"/></property>
    </bean>
    -->

    <!-- Refers to the combined database here -->
    <!-- (see dataAccessContext-jta.xml for an alternative) -->
    <bean id="sequenceDao" class="org.springframework.samples.jpetstore.dao.i batis.SqlMapSequenceDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMapClient"><ref local="sqlMapClient"/></property>
    </bean>

    <!-- SequenceDao definition for Oracle databases -->
    <!-- (to be used instead of the default sequenceDao) -->
    <!--
    <bean id="sequenceDao" class="org.springframework.samples.jpetstore.dao.i batis.OracleSequenceDao">
    <property name="dataSource"><ref local="dataSource"/></property>
    <property name="sqlMap"><ref local="sqlMap"/></property>
    </bean>
    -->
    </beans>

    The SqlMapAccountDao e.g.:

    package org.springframework.samples.jpetstore.dao.ibatis;

    import java.util.List;

    import org.springframework.dao.DataAccessException;
    import org.springframework.orm.ibatis.support.SqlMapClien tDaoSupport;
    import org.springframework.samples.jpetstore.dao.AccountD ao;
    import org.springframework.samples.jpetstore.domain.Accou nt;

    /**
    * In this and other DAOs in this package, a DataSource property
    * is inherited from the SqlMapDaoSupport convenience superclass
    * supplied by Spring. DAOs don't need to extend such
    * superclasses, but it saves coding in many cases. There are
    * analogous superclasses for JDBC (JdbcDaoSupport), Hibernate
    * (HibernateDaoSupport), JDO (JdoDaoSupport) etc.
    * <p>
    * This and other DAOs are configured using Dependency Injection.
    * This means, for example, that Spring can source the DataSource
    * from a local class, such as the Commons DBCP BasicDataSource,
    * or from JNDI, concealing the JNDI lookup from application code.
    *
    * @author Juergen Hoeller
    */
    public class SqlMapAccountDao
    extends SqlMapClientDaoSupport
    implements AccountDao {

    public Account getAccount(String username) throws DataAccessException {
    return (Account) getSqlMapClientTemplate().queryForObject(
    "getAccountByUsername",
    username);
    }

    public Account getAccount(String username, String password)
    throws DataAccessException {
    Account account = new Account();
    account.setUsername(username);
    account.setPassword(password);
    return (Account) getSqlMapClientTemplate().queryForObject(
    "getAccountByUsernameAndPassword",
    account);
    }

    public void insertAccount(Account account) throws DataAccessException {
    getSqlMapClientTemplate().update("insertAccount", account);
    getSqlMapClientTemplate().update("insertProfile", account);
    getSqlMapClientTemplate().update("insertSignon", account);
    }

    public void updateAccount(Account account) throws DataAccessException {
    getSqlMapClientTemplate().update("updateAccount", account);
    getSqlMapClientTemplate().update("updateProfile", account);
    if (account.getPassword() != null
    && account.getPassword().length() > 0) {
    getSqlMapClientTemplate().update("updateSignon", account);
    }
    }

    public List getUsernameList() throws DataAccessException {
    return getSqlMapClientTemplate().queryForList("getUsernam eList", null);
    }

    }

    Maybee this helps you

    Comment


    • #3
      Re: Problem using a SqlMapClientFactoryBean

      Duke,

      This is "a bug" in your usage of the FactoryBean actually.

      You do not have to manually call getObject() on factory bean. Just refer to the bean normally, as if it is the object that it actually returns. Spring will dereference the factory bean. You are introducing an extra layer of indirection where it's actually not needed, and Spring is complaining because of course there's no getObject method on the sqlmapclient object that the factory bean has already returned.

      Just take out your "sqlMapClientSimatel" bean definition, and refer to the factory directly. You probably want to rename the factory bean too. The common convention is to name it after the object it actually returns.

      Comment


      • #4
        Ok

        Thank you for your help.

        Comment

        Working...
        X