Announcement Announcement Module
Collapse
No announcement yet.
Persistence with Spring - 'Hibernate Template' wrapper. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Persistence with Spring - 'Hibernate Template' wrapper.

    Hi all.

    I'd like to get your advice how to work around or solve such problem.
    Here is described approach for declarative transaction managment using Spring and Hibernate. That approach was used and works for 100% using Spring 1.1 + Hibernate 2.1 but I can't put it into service using Spring 1.2.3 and Hibernate 3.0.5.

    Here is the used classes hierarchy:

    1. Implementation of Top Level persist manager what is wrapper around 'Hibernate Template'.
    Code:
    // TOP LEVEL PERSIST MANAGER CLASS
    public class PersistManager
            extends HibernateDaoSupport
            implements HibernateOperations {
        public PersistManager() {
        }
        public Object execute(HibernateCallback action) throws DataAccessException {
            return getHibernateTemplate().execute(action);
        }
    ...
        public Object load(Class entityClass, Serializable id)
                throws DataAccessException {
            return getHibernateTemplate().load(entityClass, id);
        }
    .............. // ALL THE REST HibernateOperations ARE IMPLEMENTED HERE
    }
    2. Implementation of our custom Service manager class.
    Code:
    // IMPLEMENTED CUSTOM SERVICE
    public class CustomServiceImpl extends PersistManager implements CustomService {
        public ProcessServiceImpl() {
        }
        
        public CustomEntity loadCustomEntityById(Integer masterPkId) throws DataAccessException {
            CustomEntity entity = (CustomEntity) super.load(CustomEntity.class, masterPkId);
            return entity;
        }
    ....
          // SaveOrUpdate, Delete are implemeneted as well
    }
    3. Spring's XML file.
    Code:
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName">
    .....
    
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
            <property name="mappingResources">
    .............
    
        <bean id="txTransactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
            <property name="transactionManager">
                <ref bean="transactionManager"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    ..............
    
        <bean id="customService" parent="txTransactionProxy"  singleton="false">
            <property name="target">
                <bean class="com.persistence.CustomService">
                    <property name="sessionFactory">
                        <ref local="sessionFactory"/>
                    </property>
                </bean>
            </property>
        </bean>
    .......
    Everything works fine it that case. We can get 'customService' class from the ApplicationContext, use it. Transactions are managed by txTransactionProxy.

    The problem arises when I need inject one 'persist management class' into another one.

    4.
    Say, I want to used first 'manager' class as internal one for the 'second persist manager'.
    Code:
    public class CustomService2Impl extends PersistManager implements CustomService2 &#123;
    
        private CustomService customeService;
    
        public void setCustomService&#40;CustomService customService&#41; &#123;
                this.customService = &#40;CustomService&#41;customService;
        &#125;
    ........// Other business methods are implemeneted here
    &#125;
    5. The XML for it looks like that:
    Code:
        <bean id="customService2" parent="baseTransactionProxy" singleton="true" >
    		<property name="target">
                <bean class="com.persistence.CustomService2Impl">
                    <property name="sessionFactory">
                        <ref local="sessionFactory"/>
                    </property>
                    <property name="customService">
                        <ref local="customService"/>
                    </property>
                </bean>
            </property>
        </bean>
    6. The problem arises when Spring tries to inject 'customService' class into 'customService2'.

    It fails with error:
    Code:
    ERROR &#91;org.springframework.web.context.ContextLoader&#93; Context initialization failed org.springframework.beans.factory.BeanCreationException&#58; Error creating bean with name 'customService2' defined in ServletContext resource &#91;/WEB-INF/applicationContext-hibernate.xml&#93;&#58; Error setting property values; nested exception is org.springframework.beans.PropertyAccessExceptionsException&#58; PropertyAccessExceptionsExcepti
    on &#40;1 errors&#41;; nested propertyAccessExceptions are&#58; &#91;org.springframework.beans.TypeMismatchException&#58; Failed to convert property value of type &#91;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&#93; to required type &#91;com.persistence.CustomService&#93; for property 'customService'&#93;
    PropertyAccessExceptionsException &#40;1 errors&#41;
    org.springframework.beans.TypeMismatchException&#58; Failed to convert property value of type &#91;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&#93; to required type &#91;com.persistence.CustomService&#93; for property 'customService'
            at org.springframework.beans.BeanWrapperImpl.doTypeConversionIfNecessary&#40;BeanWrapperImpl.java&#58;1035&#41;
            at org.springframework.beans.BeanWrapperImpl.setPropertyValue&#40;BeanWrapperImpl.java&#58;803&#41;
    As I see Spring assumes 'CustomService2' to be type of org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean that's correct... but it doesn't consider it as my type of com.persistence.CustomService2.
    Why so ? How to solve or work around that problem?

    Describes approach WORKS for 1000% using Spring 1.1 and Hibernate 2.1. Spring correctly avaluates type of com.persistence.CustomService2 but it doesn't in 1.2.3 version and Hibernate 3.x

    Thanks in advance.

  • #2
    Two things to check:

    1. Do you have multiple versions of Spring on the classpath?
    2. Does this work with the new snapshot from CVS?

    Rob

    Comment


    • #3
      Thanks for suggestion, Rob.

      Originally posted by robh
      1. Do you have multiple versions of Spring on the classpath?
      Nop, I have only one spring.jar in classpath.
      2. Does this work with the new snapshot from CVS?
      It started work for me when I changed XML description like that:

      Code:
          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
              <property name="sessionFactory">
                  <ref local="sessionFactory"/>
              </property>
          </bean>
      
      	<bean id ="CustomServiceTarget" class="com.persistence.CustomServiceImpl"> 
              <property name="sessionFactory">
      			<ref bean="sessionFactory"/>
              </property>
      	</bean>
      
      	<bean id="customService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      		<property name="transactionManager">
      			<ref bean="transactionManager" />
      		</property>
      		<property name="target">
      			<ref bean="CustomServiceTarget" />
      		</property>
      		<property name="transactionAttributes">
      			<props>
      				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
      				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
      				<prop 
      .............................................
      
      			</props>
      		</property>
      	</bean>
      Everything works on 1.2.1, 1.2.3 and last build spring-framework-1.2.4-build.322-20050809.zip

      Now I can inject another described component into the first one.

      Comment


      • #4
        That's quite odd... Could you please post the full stacktrace of the issue you originally encountered? Even if a change in configuration style works for you now, I'd like to track down what the root cause of the original issue was (assuming that it's not multiple versions of spring.jar on the classpath, which could cause such issues).

        Juergen

        Comment


        • #5
          The same problem again

          Hello, I noticed the same problem again:

          org.springframework.beans.TypeMismatchException: Failed to convert property value of type [org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean] to required type [my.application.MyInterface] for property 'myProperty'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.transaction.interceptor.Transa ctionProxyFactoryBean] to required type [com.mtvi.dextr.Dextr] for property 'myProperty'
          : no matching editors or conversion strategy found


          Details about the problem: I'm working on EAR deployment which includes number of WAR's. I'm deploying them on JBoss 4.0.5. Version of Spring used is 2.5.5, Hibernate 3.2.3.GA.

          Note: the problem appears only when I remove spring JARs from nested WAR's (their WEB-INF/lib folders) and put them in outer EAR as libraries registered in application.xml. The problem disappears if I duplicate the spring JAR's in the nested WAR's (their WEB-INF/lib folders).

          Could you please advise how can I keep Spring JAR's only in EAR libraries and have them working correctly with WAR packages nested in the EAR?

          Thank you in advance.

          Comment


          • #6
            hey

            First time i come to Spring community forum site.this thread is amazing.i'll be post soon..
            data entry services india

            Comment

            Working...
            X