Announcement Announcement Module
Collapse
No announcement yet.
Autowiring don't work when PersistenceExceptionTranslationPostProcessor is declared Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowiring don't work when PersistenceExceptionTranslationPostProcessor is declared

    Hi, I need to use the PersistenceExceptionTranslationPostProcessor bean to translate Hibernate exceptions into Spring exceptions.

    Before I do not have this translation and everything works, but now I have an autowiring error.

    Here is my simple test which fails with the PersistenceExceptionTranslationPostProcessor bean declared and succeed without it.

    I'm trying to test a DAO class:
    Code:
    package com.thales.imp.server.dao.hbm;
    
    ...
    ...
    
    @ContextConfiguration(
        locations =
            {
                "classpath:/com/thales/imp/server/server-commons.beans.xml",
                "classpath:/com/thales/imp/server/server-shared-test.beans.xml"
            }
    )
    public class AbstractHibernateDaoSupportTest
        extends AbstractTransactionalJUnit4SpringContextTests
    {
     ...
    
     @Autowired
     private HibernateDaoSupportImpl hibernateDaoSupport;
    
     ....
    
        @Repository
        public static class HibernateDaoSupportImpl
            extends AbstractHibernateDaoSupport<ImpDataImpl1, Long>
        {
            public HibernateDaoSupportImpl()
            {
                super();
            }
        }
    
     }
    The XML Spring files contains:


    -- server-commons.beans.xml --

    Code:
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
    <context:annotation-config />
    <context:component-scan base-package="com.thales.imp.server.dao.hbm" />
    The server-shared-test.beans.xml declares a dataSource, a transactionManager and an Hibernate session factory.


    If, in "server-commons.beans.xml" there is not any PersistenceExceptionTranslationPostProcessor bean declaration the bean "HibernateDaoSupportImpl " is found, instantiated and my tests succeed.

    If PersistenceExceptionTranslationPostProcessor is declared the following exception is thrown:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest.hibernateDaoSupport; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl] is defined: Unsatisfied dependency of type [class com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl]: expected at least 1 matching bean
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:231)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:978)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:291)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:127)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
    	at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    	at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    	at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest.hibernateDaoSupport; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl] is defined: Unsatisfied dependency of type [class com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl]: expected at least 1 matching bean
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredElement.inject(AutowiredAnnotationBeanPostProcessor.java:375)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:61)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:228)
    	... 18 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl] is defined: Unsatisfied dependency of type [class com.thales.imp.server.dao.hbm.AbstractHibernateDaoSupportTest$HibernateDaoSupportImpl]: expected at least 1 matching bean
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveDependency(AbstractAutowireCapableBeanFactory.java:417)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredElement.inject(AutowiredAnnotationBeanPostProcessor.java:361)
    	... 20 more
    So, do you know what is the problem ?

    Thanks.

  • #2
    The PersistenceExceptionTranslationPostProcessor will create a proxy around your HibernateDaoSupportImpl object. Does the AbstractHibernateDaoSupport class implement any interfaces? If so, Spring will proxy those interface(s), and the resulting object type will not be HibernateDaoSupportImpl.

    Comment


    • #3
      The PersistenceExceptionTranslationPostProcessor will create a proxy around your HibernateDaoSupportImpl object. Does the AbstractHibernateDaoSupport class implement any interfaces? If so, Spring will proxy those interface(s), and the resulting object type will not be HibernateDaoSupportImpl.
      The AbstractHibernateDaoSupport class is the following:
      Code:
      public abstract class AbstractHibernateDaoSupport<T extends ImpData, ID extends Serializable>
          extends HibernateDaoSupport {
      ...
      ...
      }
      So, yes the class org.springframework.orm.hibernate3.support.Hiberna teDaoSupport implements the org.springframework.beans.factory.InitializingBean , is it a problem ?

      If yes, how can I use the PersistenceExceptionTranslationPostProcessor with my HibernateDaoSupportImpl class to do automatic exception translation ?

      Thanks.

      Comment


      • #4
        Since your DAO support class extends HibernateDaoSupport, are you using a HibernateTemplate? If yes, why do you need a PersistenceExceptionTranslationPostProcessor?

        One option is to eliminate the concrete inheritance on HibernateDaoSupport, and implement a native Hibernate DAO instead, as described here:

        http://blog.springsource.com/main/20...r-jpatemplate/

        You could also use the PersistenceExceptionTranslationInterceptor instead of the PersistenceExceptionTranslationPostProcessor, which will give you more explicit over how the proxy is created. For example:

        Code:
        <bean id="persistenceExceptionInterceptor" class="org.springframework.dao.support.PersistenceExceptionTranslationInterceptor" />
        
        <aop:config proxy-target-class="true">
        	<aop:advisor pointcut="execution(* @org.springframework.stereotype.Repository *.*(..))" advice-ref="persistenceExceptionInterceptor" />
        </aop:config>

        Comment


        • #5
          Since your DAO support class extends HibernateDaoSupport, are you using a HibernateTemplate? If yes, why do you need a PersistenceExceptionTranslationPostProcessor?
          No, i'm not using a HibernateTemplate because I need to have access to the Hibernate Session API to use Query, Criteria... functionnalities. So, because i'm directly using Hibernate I need to do DAO exception translation.

          One option is to eliminate the concrete inheritance on HibernateDaoSupport, and implement a native Hibernate DAO instead, as described here:

          http://blog.springsource.com/main/20...r-jpatemplate/
          Ok, I eliminate the inheritance on HibernateDaoSupport and it works now.

          Thanks for your help !

          Comment

          Working...
          X