我发现Spring数据1.6.0的奇怪行为(降级版本1.5.2没有这个问题)。看来这个版本严格要求实体管理器工厂bean的id为" entityManagerFactory"。如果没有,运行TestNG测试时会出现此错误:
引起:org.springframework.beans.factory.BeanCreationException:创建名称为' jpaMapppingContext的bean时出错':无法创建内部bean'(内部bean)#36b87404'类型为[org.springframework.orm.jpa.SharedEntityManagerCreator]时设置bean属性' entityManager&#39 ;;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'(内部bean)的bean时出错#36b87404':无法解析对bean的引用' entityManagerFactory'设置构造函数参数时;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有bean命名为' entityManagerFactory'已定义
我的配置是:
<!-- ************************************************************** -->
<!-- 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更改为&#34; entityManagerFactory&#34;而不是&#34; entityManagerFactoryCustomId&#34;。
答案 0 :(得分:2)
刚刚添加了@StéphaneNicoll答案: 然后显式设置实体管理器(请参阅jpa:repositories中的entity-manager-factory-refelement。看起来文档可能已过时。