java - 没有名为 testPU 的 EntityManager 的持久性提供程序

标签 java hibernate jpa junit entitymanager

我正在 JBoss 7.1.0 上使用 Guice 作为 DI 框架开发一个小型应用程序,并使用 maven 构建它。 我所有的 DAO 都将 EntityManager 作为构造函数注入(inject)注入(inject):

public class MyDao{
...
@Inject
public MyDao(EntityManager em){}
...
}

为了测试这些类,我需要在测试中创建一个 EntityManager。

为了创建 EntityManager,我将 persistence.xml 添加到 src/test/resources/META-INF/persistence.xml

我的 persistence.xml 如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.foo.Foo</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:unit-testing-jpa" />
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />
        </properties>
    </persistence-unit>
</persistence>

在我的测试设置方法中,我执行以下操作:

private EntityManager em;

@Before
public void setup() {
    em = Persistence.createEntityManagerFactory("testPU")
            .createEntityManager();
}

但我总是收到错误“没有名为 testPU 的 EntityManager 的持久性提供程序”。 (在eclipse中运行测试并使用maven)

我不知道我做错了什么。你能帮我吗?

提前致谢!

最佳答案

添加 hibernate-entitymanager 依赖项,范围为 providedtest(我需要有关项目的更多信息来确定范围)

  <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>...</version>
     <scope>provided</scope>
  </dependency>  
PS。它仅适用于 JBoss。例如,对于 WebLogic 范围应该是 compile

关于java - 没有名为 testPU 的 EntityManager 的持久性提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12216843/

相关文章:

java - orphanRemoval 在 hibernate 状态下无法正常工作 : Element inserted and removed from list is persisted in DB

java - 从hibernate中的sessionfactory实体类中检索主键

java - 没有名为 ws-persist 的 EntityManager 持久性提供程序

java - 执行hdfs namenode -format的异常

java - 将列表存储在表列中

java - 应用程序卡在 hibernate 状态 txOracle.commit();

java - Hibernate FETCH.Eager 在使用 Spring Data 存储库时获取重复项

java - 如何使用从 Java 中的方法返回的数组

java - Orika - 不生成字段映射;仅按默认工作

Java:通配符数组类型可以同时用作参数和返回类型吗?