我正在 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
依赖项,范围为 provided
或 test
(我需要有关项目的更多信息来确定范围)
<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/