未定义 [javax.persistence.EntityManager] 类型的唯一 bean

发布于 2024-09-03 13:34:36 字数 2613 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-09-10 13:34:36

您的 Spring 上下文有一个使用 LocalContainerEntityManagerFactoryBean 的 bean 定义。这将创建一个 EntityManagerFactory,而不是 EntityManager

AssignmentDao 需要将自身与 EntityManagerFactory 连接起来。

或者,您可以将 LocalContainerEntityManagerFactoryBean 替换为 LocalEntityManagerFactoryBean,这将直接创建 EntityManager。但是,您需要小心这一点,它有一些缺点。请参阅 Spring 文档 有关选项的完整说明。

这很令人困惑,因为 JPA 和 Spring 的命名约定相互重叠,因此命名这些类是一个真正的问题。

Your Spring context has a bean definition using LocalContainerEntityManagerFactoryBean. This creates an EntityManagerFactory, not an EntityManager.

AssignmentDao needs to get itself wired with an EntityManagerFactory.

Alternatively, you can replace the LocalContainerEntityManagerFactoryBean with a LocalEntityManagerFactoryBean, which will create an EntityManager 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文