TerenceJackson
TerenceJackson

Reputation: 1786

No persistence provider for EntityManager named testPU

I'm developing a small application on JBoss 7.1.0 using Guice as DI Framework and I'm building it using maven. All my DAO's get the EntityManager injected as constructor Injection:

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

To test these classes I need to create an EntityManager in my tests.

To create the EntityManager I added my persistence.xml to src/test/resources/META-INF/persistence.xml

My persistence.xml looks like this:

<?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>

In my setup method of the tests I do the following:

private EntityManager em;

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

But I always get the error "No persistence provider for EntityManager named testPU". (Running the tests in eclipse and using maven)

I cannot figure out what I am doing wrong. Can you please help me?

Thx in advance!

Upvotes: 1

Views: 2129

Answers (1)

Ilya
Ilya

Reputation: 29673

add hibernate-entitymanager dependency with scope provided or test (I need more info about project to determine scope)

  <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-entitymanager</artifactId>
     <version>...</version>
     <scope>provided</scope>
  </dependency>  

PS. It's only for JBoss. For example, for WebLogic scope should be compile

Upvotes: 2

Related Questions