Announcement Announcement Module
Collapse
No announcement yet.
can't use @Transaction-annotation in combinations with a @Reousrce-annotations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • can't use @Transaction-annotation in combinations with a @Reousrce-annotations

    I'm using a hibernate based DAO class when I place @Transaction-annotation on some of it's methods spring gets the following exception:

    Code:
    org.springframework.beans.factory.BeanCreationException: 
    Error creating bean with name 'myService': 
    Injection of resource methods failed; nested exception is
    org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
    Bean named 'myDAO' must be of type [my.data.MyDAO], 
    but was actually of type [$Proxy13]
    It looks like some proxy is created when using the @Transaction-annotation and the springframework can't handle it. Everything works fine when I remove the @Transaction-annotation.

    My spring and hibernate configuration:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/tx
               http://www.springframework.org/schema/tx/spring-tx.xsd
               ">
       <context:annotation-config/>
       <context:component-scan base-package="my"/>
    
    
       <bean id="my_dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="${my.jdbc_driverClass}"/>
          <property name="url" value="${my.jdbc_url}"/>
          <property name="username" value="${my.jdbc_username}"/>
          <property name="password" value="${my.jdbc_password}"/>
       </bean>
    
       <bean id="my_sessionFactory"
             class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
          <property name="dataSource" ref="financial_dataSource"/>
          <property name="annotatedPackages">
             <list>
                <value>my.model</value>
             </list>
          </property>
          <property name="annotatedClasses">
             <list>
                <value>my.model.Object1</value>
             </list>
          </property>
    
          <property name="schemaUpdate" value="true"/>
          <property name="hibernateProperties">
             <props>
                <prop key="hibernate.dialect">${my.hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${my.hibernate.showSQL}</prop>
                <prop key="hibernate.hbm2ddl.auto">${my.hibernate.autoDDL}</prop>
             </props>
          </property>
    
       </bean>
    
       <!-- setup transaction manager        -->
       <bean id="transactionManager"
             class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory">
             <ref local="my_sessionFactory"/>
          </property>
       </bean>
    
       <tx:annotation-driven transaction-manager="transactionManager"/>
    
    </beans>
    My Dao class:
    Code:
    public class MyDAO<T, ID extends Serializable>
    {
       private SessionFactory sessionFactory;
    
       @SuppressWarnings("unchecked")
       @Transactional(propagation = Propagation.REQUIRED)
       public ID store(T instance)
       {
          if (log.isDebugEnabled())
          {
             log.debug("storing: " + instance);
          }
          return (ID) getSession().save(instance);
       }
    
       protected Session getSession()
       {
          return getSessionFactory().getCurrentSession();
       }
    
       private SessionFactory getSessionFactory()
       {
          return sessionFactory;
       }
    
       @Resource
       public void setSessionFactory(SessionFactory sessionFactory)
       {
          this.sessionFactory = sessionFactory;
       }
    
    
    }
    My service getting the myDao injected:
    Code:
    @Service
    public class MyService
    {
       private MyDAO myDAO;
    
       public MyDAO getMyDAO()
       {
          return myDAO;
       }
    
       @Resource
       public void setMyDAO(MyDAO myDAO)
       {
          this.myDAO = myDAO;
       }

  • #2
    I found the solution by setting the proxy-target-class property on the annotaion-driven element. In de spring bean configuration.

    Code:
      <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"  />

    Comment

    Working...
    X