2011-07-29 4 views
0

Bonjour, J'ai eu un problème avec l'intégration ci-dessus des cadres.Problème de transaction avec JPA2-spring-Hibernate

J'utilise JPA2 dans une configuration Maven avec les versions de la bibliothèque:

  • Spring 3.0.5
  • Hibernate 3.5.4
  • Hibernate JPA-2.0-api-1.0.0. finale

J'utilise le persistence.xml:

<persistence-unit name="AccountingPU" transaction-type="RESOURCE_LOCAL"> 
     <provider>org.hibernate.ejb.HibernatePersistence</provider> 
     <class>com.westernacher.wps.bnotk.fibu.domain.BaseEntity</class> 
     <class>com.westernacher.wps.bnotk.fibu.domain.AccountSettings</class> 
     <!-- This is not any more necessary, it will be done in spring config. 
     <properties> 
      <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"/> 
     </properties> --> 
    </persistence-unit> 

et applicationContext.xml:

<context:annotation-config /> 
    <context:component-scan base-package="com.westernacher.wps.bnotk.fibu.dao" /> 
    <context:component-scan base-package="com.westernacher.wps.bnotk.fibu.service" /> 
<tx:annotation-driven transaction-manager="txManager" 
    proxy-target-class="false" 
    mode="proxy" /> 

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" 
    p:dataSource-ref="dataSource"> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
      <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
     </props> 
    </property> 
</bean> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
    p:driverClassName="${dataSource.driverClassName}" p:url="${dataSource.url}" 
    p:username="${dataSource.username}" p:password="${dataSource.password}" /> 

<context:property-placeholder location="classpath:datasource.properties" /> 

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager" 
    p:entityManagerFactory-ref="entityManagerFactory" /> 

La AccountSettingsDaoImpl classe est annotés avec @Repository. Les méthodes d'accès à la base de cette catégorie sont marqués par @Transactional:

@Transactional(readOnly=false) 
    public void save(E entity) { 
//  entityManager.merge(entity); 
//  entityManager.flush(); 
     entityManager.persist(entity); 
    } 

La classe de test est la suivante:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("/applicationContext.xml") 
public class AccountSettingsDaoImplTest { 

    @Resource 
    private AccountSettingsDaoImpl accountSettingsDaoImpl; 

    private final static Long id=1L; 

    @Test 
    public void testSave() throws Exception { 
     AccountSettings accountSettings = new AccountSettings(); 
     accountSettings.setCategory("A"); 
     accountSettings.setDescription("adlkjsadhad asdlkasdkjahsdlkashd "); 
     accountSettingsDaoImpl.save(accountSettings); 

     Long localId = accountSettings.getId(); 
     System.out.println("id="+localId); 
    } 
} 

Mais courir mes JUnit-tests, je vais faire l'exception suivante:

29.07.2011 18:46:01 org.springframework.test.context.TestContextManager prepareTestInstance 
SCHWERWIEGEND: Caught exception while allowing TestExecutionListener [org.springframewor[email protected]127e942f] to prepare test instance [[email protected]b9c18ae] 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.westernacher.wps.bnotk.fibu.dao.AccountSettingsDaoImplTest': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountSettingsDaoImpl' must be of type [com.westernacher.wps.bnotk.fibu.dao.AccountSettingsDaoImpl], but was actually of type [$Proxy23] 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:300) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'accountSettingsDaoImpl' must be of type [com.westernacher.wps.bnotk.fibu.dao.AccountSettingsDaoImpl], but was actually of type [$Proxy23] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:349) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:435) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:409) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:541) 
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:297) 
    ... 26 more 
29.07.2011 18:46:01 org.springframework.context.support.AbstractApplicationContext doClose 

Quelqu'un peut-il m'aider?

Cordialement, Daniel

Répondre

2

Vous ne pouvez pas injecter des procurations par type concret (si elles sont faites par l'interface) - vous devez utiliser l'interface à la place:

@Inject 
private AccountSettingsDao accountSettingsDao; 
+0

wow, cette astuce était merveilleux et si rapide. Cela a fait fonctionner. Merci beaucoup. – DTU

Questions connexes