我正在使用 Guice 作为 DI 框架在 JBoss 7.1.0 上开发一个小型应用程序,并且我正在使用 maven 构建它。我所有的 DAO 都将 EntityManager 作为构造函数注入注入:
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)
我无法弄清楚我做错了什么。你能帮我么?
提前谢谢!