我发现
Spring数据1.6.0的奇怪行为(降级版本1.5.2没有这个问题).看来这个版本严格要求实体管理器工厂bean的id为“entityManagerFactory”.如果没有,运行TestNG测试时会出现此错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘jpaMapppingContext’: Cannot create inner bean ‘(inner bean)#36b87404’ of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property ‘entityManager’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘(inner bean)#36b87404’: Cannot resolve reference to bean ‘entityManagerFactory’ while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘entityManagerFactory’ is defined
我的配置是:
<!-- ************************************************************** -->
<!-- Database configuration -->
<!-- ************************************************************** -->
<!-- Entity manager factory bean -->
<bean id="entityManagerFactoryCustomId"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="test-system" />
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<array>
<value>${pds.db.scan.model}</value>
</array>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">${jdbc.showsql}</prop>
<prop key="hibernate.format_sql">${jdb.formatsql}</prop>
<prop key="hibernate.hbm2ddl.auto">${jdbc.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<!-- C3P0 connection pool -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<!-- Connection properties -->
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Pool properties -->
<property name="minPoolSize" value="${pool.minsize}" />
<property name="maxPoolSize" value="${pool.maxsize}" />
<property name="initialPoolSize" value="${pool.initialPoolSize}" />
<property name="maxStatements" value="${pool.maxstatements}" />
<property name="acquireIncrement" value="${pool.acquireincrement}" />
<property name="preferredTestQuery" value="${jdbc.check}" />
<property name="numHelperThreads" value="${pool.threads}" />
</bean>
<!-- JPA transaction manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryCustomId" />
</bean>
<!-- Activate Spring Data JPA repository support -->
<jpa:repositories base-package="pds.archiva.db.repository" />
</code>
它是Spring Data JPA中的错误还是我做错了什么?在Windows java 64bit 7u55上测试,具有以下版本:
spring.framework.version = 4.0.5.RELEASE
spring.security.version = 3.2.4.RELEASE
spring.data-jpa.version = 1.6.0.RELEASE
正如我所写,相同的测试仅使用spring.data-jpa.version = 1.5.2.RELEASE …或将bean的id更改为“entityManagerFactory”而不是“entityManagerFactoryCustomId”.
最佳答案 刚刚添加@StéphaneNicoll答案:
然后显式设置实体管理器(请参阅jpa:repositories中的entity-manager-factory-refelement.看起来文档可能已过时.