【问题标题】:JPA Entities not scanned during unit test单元测试期间未扫描 JPA 实体
【发布时间】:2013-05-16 19:24:51
【问题描述】:

我有一个使用 Hibernate/JPA 的成熟 Java 应用程序,它运行良好。我们正在尝试添加一些单元/集成测试。我正在使用 Spring 的 TestContext 框架执行此操作,我的测试类是这样的:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ServiceTest {

    @Autowired
    private MyService myService;

    @Test
    public void testWorkspaceThing() throws Exception {

        List<MyEntity> entities = myService.someMethod();
        assertNotNull(entities);
    }
}

我将所有必要的上下文配置从应用程序上下文复制/粘贴到 ServiceTest-context.xml。这包括 context:component-scan、定义 dataSource、entityManagerFactory beans 等。当应用程序运行时,我没有错误。运行此测试时,我得到:

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: MyEntity is not mapped.

对于我尝试查询的任何和所有实体类。 Entity 类使用 javax.persistence.Entity 等注解。

有谁知道为什么使用 TestContext 会失败?

更新

我们的 persistence.xml 文件本质上是空的:

<persistence-unit name="some-pu" />

所以我假设它会自动扫描以查找所有带注释的实体类。在运行单元测试时,它在 tomcat 中的部署方式与文件夹结构之间的目录结构可能有所不同吗?我们的 persistence.xml 在 WebContent/WEB-INF/classes/META-INF

另外 - 我注意到您可以使用 Spring 3.1 定义“packagesToScan”,但我们使用的是 Spring 3.0.6.RELEASE

【问题讨论】:

    标签: java spring hibernate jpa junit


    【解决方案1】:

    此消息通常表示您尚未在 hibernate.cfg 或 persistence.xml 中映射实体

    <mapping class="com.yourpackage.MyEntity" />
    ...other entities
    

    确保MyEntity类被映射到那里,并且它在类中的注解正好是@Entity(name="MyEntity")

    编辑:如果您使用上面的 Spring 3.1,请尝试在您的测试上下文中指示 EntityManager 工厂 bean:

      <property name="packagesToScan">
         <list>
            <value>com.yourpackage.domain</value>
         </list>
      </property>
    

    【讨论】:

    • 查看我上面的更新,persistence.xml 非常简单。部署在 tomcat 里面就可以了。
    • 不幸的是,我们使用的是 3.0.6。我想知道是否有任何方法可以在不升级的情况下解决此问题。
    【解决方案2】:

    我通过 ant 运行 junit 测试使其工作。我将 persistence.xml 文件复制到 build/classes/META-INF 中,以便它与已编译的实体类位于同一文件夹树中,这样就可以进行扫描并正确运行测试。

    如果我删除 persistence.xml 的 Webcontent/WEB-INF/classes/META-INF 副本并将其保留在 build/ 文件夹中,这在 Eclipse 中有效。我想知道是否有更好的方法让它在 Eclipse 中工作。

    【讨论】:

      【解决方案3】:

      尝试将其添加到您的 persistence.xml 文件中。

      <persistence-unit name="some-pu" transaction-type="RESOURCE_LOCAL">
      <class>package.MyEntity</class>
      
              <properties>
              <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
              <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
              <property name="hibernate.connection.username" value="USER-DB"/>
              <property name="hibernate.connection.password" value="PASSWORD-DB"/>
              <property name="hibernate.connection.url" value="jdbc:mysql://SERVER-IP/some-pu?autoReconnect=true"/>
              <property name="hibernate.max_fetch_depth" value="3"/>
      
               <!-- Determines how many connections at a time c3p0 will try to acquire when the pool is exhausted. :: default=1 -->
              <property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
              <property name="hibernate.c3p0.min_size" value="2"/>
              <property name="hibernate.c3p0.timeout" value="300"/>
              <property name="hibernate.c3p0.idle_test_period" value="300"/>
              <property name="hibernate.c3p0.acquire_increment" value="1"/> 
              <property name="hibernate.c3p0.max_size" value="10"/> 
              <property name="hibernate.c3p0.max_statements" value="50"/>     
      
          </properties>
      </persistence-unit>
      

      希望对你有帮助

      【讨论】:

      • 感谢您的帮助,但我想让它自动扫描 - 我们有大量实体。
      • 您尝试指定您的弹簧上下文?在您需要添加的@ContextConfiguration 行上:({Spring-context})。所以结果是@ContextConfiguration({Spring-context})
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 1970-01-01
      • 2017-07-22
      • 1970-01-01
      相关资源
      最近更新 更多