未定义 [javax.persistence.EntityManager] 类型的唯一 bean
我正在使用 JUnit 4 来测试带有 Spring(注释)和 JPA(休眠)的 Dao Access。数据源通过 JNDI(Weblogic) 和 ORacle(Backend) 进行配置。此持久性仅使用名称和 RESOURCE_LOCAL 事务类型进行配置。
应用程序上下文文件包含注释、JPA 配置、事务以及用于注释检测的默认包和配置的注释。 我正在使用 Junit4,如下所示:
ApplicationContext
<代码>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="workRequest"/>
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${database.target}"/>
<property name="showSql" value="${database.showSql}" />
<property name="generateDdl" value="${database.generateDdl}" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>workRequest</value>
</property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://localhost:7001</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
JUnit 测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class AssignmentDaoTest {
private AssignmentDao assignmentDao;
@Test
public void readAll() {
assertNotNull("assignmentDao cannot be null", assignmentDao);
List<Assignment> assignments = assignmentDao.findAll();
assertNotNull("There are no assignments yet", assignments);
}
}
无论我做出什么改变,我都会得到:
<代码>
未定义 [javax.persistence.EntityManager] 类型的唯一 bean 。
任何关于这可能是什么的提示 我正在 eclipse 中运行测试。
I am using JUnit 4 to test Dao Access with Spring (annotations) and JPA (hibernate). The datasource is configured through JNDI(Weblogic) with an ORacle(Backend). This persistence is configured with just the name and a RESOURCE_LOCAL transaction-type
The application context file contains notations for annotations, JPA config, transactions, and default package and configuration for annotation detection.
I am using Junit4 like so:
ApplicationContext
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="workRequest"/>
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="${database.target}"/>
<property name="showSql" value="${database.showSql}" />
<property name="generateDdl" value="${database.generateDdl}" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>workRequest</value>
</property>
<property name="jndiEnvironment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://localhost:7001</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
JUnit TestCase
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class AssignmentDaoTest {
private AssignmentDao assignmentDao;
@Test
public void readAll() {
assertNotNull("assignmentDao cannot be null", assignmentDao);
List<Assignment> assignments = assignmentDao.findAll();
assertNotNull("There are no assignments yet", assignments);
}
}
regardless of what changes I make I get:
No unique bean of type [javax.persistence.EntityManager] is defined
Any hint on what this could be. I am running the tests inside eclipse.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 Spring 上下文有一个使用 LocalContainerEntityManagerFactoryBean 的 bean 定义。这将创建一个
EntityManagerFactory
,而不是EntityManager
。AssignmentDao
需要将自身与EntityManagerFactory
连接起来。或者,您可以将
LocalContainerEntityManagerFactoryBean
替换为LocalEntityManagerFactoryBean
,这将直接创建EntityManager
。但是,您需要小心这一点,它有一些缺点。请参阅 Spring 文档 有关选项的完整说明。这很令人困惑,因为 JPA 和 Spring 的命名约定相互重叠,因此命名这些类是一个真正的问题。
Your Spring context has a bean definition using
LocalContainerEntityManagerFactoryBean
. This creates anEntityManagerFactory
, not anEntityManager
.AssignmentDao
needs to get itself wired with anEntityManagerFactory
.Alternatively, you can replace the
LocalContainerEntityManagerFactoryBean
with aLocalEntityManagerFactoryBean
, which will create anEntityManager
directly. However, you need to be careful with that one, it has some downsides. See that part of the Spring docs for a full explanation of the options.It's confusing, because the naming conventions of JPA and Spring overlap each other, so naming these classes is a real bugger.