Announcement Announcement Module
Collapse
No announcement yet.
JdbcDaoSupport and HibernateDaoSupport in single class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Hi,

    We don't use Hibernate in our project, we use Apache OJB but we had to use PersistenceBrokerTemplate and JdbcTemplate in one Dao as well. That is my solution that works fine with Spring tx support. It should be the same using Hibernate.

    This is the base Dao class:
    Code:
    public abstract class PersistenceBrokerAndJdbcDaoSupport extends PersistenceBrokerDaoSupport
    {
    
      // mimic JdbcDaoSupport here
      private JdbcTemplate jdbcTemplate = createJdbcTemplate();
    
      protected JdbcTemplate createJdbcTemplate()
      {
        return new JdbcTemplate();
      }
    
      public void setDataSource(DataSource dataSource)
      {
        getPersistenceBrokerTemplate().setDataSource(dataSource);
        getJdbcTemplate().setDataSource(dataSource);
      }
    
      public JdbcTemplate getJdbcTemplate()
      {
        return jdbcTemplate;
      }
    
      public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
      {
        this.jdbcTemplate = jdbcTemplate;
        this.jdbcTemplate.setDataSource(getPersistenceBrokerTemplate().getDataSource());
      }
    }
    This is some Dao class:
    Code:
    public class SomeDaoImpl extends PersistenceBrokerAndJdbcDaoSupport implement SomeDao
    {
    
      public void updateObject(Object obj) throws DataAccessException
      {
        getPersistenceBrokerTemplate().store(obj);
      }
    
      public void deleteObject(Long id) throws DataAccessException
      {
        getJdbcTemplate().update("delete from t_object where id = ?",
                                     new Object[] { id },
                                     new int[] { Types.INTEGER });
      }
    }
    This is dataAccessContext.xml
    Code:
    <!-- Configure in OJB:
    ConnectionFactoryClass=org.springmodules.orm.ojb.support.LocalDataSourceConnectionFactory-->
      <bean name="ojbConfigurer" class="org.springmodules.orm.ojb.support.LocalOjbConfigurer"/>
    
      <bean name="tx.manager.parent" abstract="true"
            class="org.springmodules.orm.ojb.PersistenceBrokerTransactionManager"/>
    
      <bean name="tx.manager.base" parent="tx.manager.parent">
        <property name="dataSource" ref="ds.base"/>
        <property name="jcdAlias" value="ds.base"/>
      </bean>
    
      <bean name="dao.base.parent" abstract="true">
        <property name="dataSource" ref="ds.base"/>
        <property name="jcdAlias" value="ds.base"/>
      </bean>
    
      <bean name="someDao" parent="dao.base.parent"
            class="SomeDaoImpl"/>
    This is applicationContext.xml
    Code:
      <bean name="service.parent" abstract="true"
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionAttributes">
          <props>
            <prop key="*">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,timeout_120</prop>
          </props>
        </property>
      </bean>
    
      <bean name="service.base.parent" parent="service.parent" abstract="true">
        <property name="transactionManager" ref="tx.manager.base"/>
      </bean>
    
      <bean name="someService" parent="service.base.parent">
        <property name="target">
          <bean class="SomeServiceImpl">
            <property name="dao" ref="someDao"/>
          </bean>
        </property>
      </bean>
    That's it. Now you can call someService through SomeService interface and access your data using OJB and JDBC within one transaction. I did not show ObjectServiceImpl code because it just uses injected Dao class through SomeDao interface and that's it.

    Hope that helps.

    Comment

    Working...
    X