2
votes

The scenario is as follows. I'm doing a J2EE project on Eclipse. The project was created with Maven. I'm strying to get my Unit tests up and running and when I try to create the EntityManager, it throws an exception:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named primary.

@BeforeClass
public static void setUpClass() throws Exception {
    Logger.getLogger("org").setLevel(Level.ALL);

    EntityManagerFactory factory = Persistence.createEntityManagerFactory("primary.");
        em = (EntityManager) factory.createEntityManager();
}

So I checked the runtime class path, by getting the URLClassLoader from the Persistent class, which includes:

/Volumes/SVEN/dev/eclipse-workspace/e-virtual/ejb/target/test-classes/ /Volumes/SVEN/dev/eclipse-workspace/e-virtual/ejb/target/classes/ /Volumes/SVEN/dev/jar_repo/mysql-connector-java-5.1.42/mysql-connector-java-5.1.42-bin.jar

The META-INF directory sits right where it belongs, in:

e-virtual/ejb/target/test-classes/META-INF/persistence.xml and e-virtual/ejb/target/classes/META-INF/persistence.xml

I only seem to be able to access the persistence.xml through: System.out.println(getClass().getResource("../../../../../META-INF/persistence.xml"));

This really gets me flabbergasted. What am I missing here?

I've already cleaned my built twice. I'm really at a miss here.

Here's the full dump of the class path:

Here's the full list:

/Volumes/SVEN/dev/eclipse-workspace/e-virtual/ejb/target/test-classes/
/Volumes/SVEN/dev/eclipse-workspace/e-virtual/ejb/target/classes/
/Volumes/SVEN/dev/jar_repo/mysql-connector-java-5.1.42/mysql-connector-java-5.1.42-bin.jar
/Users/sven/.m2/repository/org/jboss/spec/javax/ejb/jboss-ejb-api_3.1_spec/1.0.2.Final/jboss-ejb-api_3.1_spec-1.0.2.Final.jar
/Users/sven/.m2/repository/javax/enterprise/cdi-api/1.0-SP4/cdi-api-1.0-SP4.jar
/Users/sven/.m2/repository/org/jboss/spec/javax/interceptor/jboss-interceptors-api_1.1_spec/1.0.1.Final/jboss-interceptors-api_1.1_spec-1.0.1.Final.jar
/Users/sven/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar
/Users/sven/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar
/Users/sven/.m2/repository/org/hibernate/hibernate-validator/4.2.0.Final/hibernate-validator-4.2.0.Final.jar
/Users/sven/.m2/repository/javax/validation/validation-api/1.0.0.GA/validation-api-1.0.0.GA.jar
/Users/sven/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar
/Users/sven/.m2/repository/junit/junit/4.11/junit-4.11.jar
/Users/sven/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
/Users/sven/.m2/repository/org/jboss/arquillian/junit/arquillian-junit-container/1.1.0.Final/arquillian-junit-container-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/junit/arquillian-junit-core/1.1.0.Final/arquillian-junit-core-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/test/arquillian-test-api/1.1.0.Final/arquillian-test-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/core/arquillian-core-api/1.1.0.Final/arquillian-core-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/test/arquillian-test-spi/1.1.0.Final/arquillian-test-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/core/arquillian-core-spi/1.1.0.Final/arquillian-core-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-test-api/1.1.0.Final/arquillian-container-test-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-api/1.1.2/shrinkwrap-api-1.1.2.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-test-spi/1.1.0.Final/arquillian-container-test-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/core/arquillian-core-impl-base/1.1.0.Final/arquillian-core-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/test/arquillian-test-impl-base/1.1.0.Final/arquillian-test-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-impl-base/1.1.0.Final/arquillian-container-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/config/arquillian-config-api/1.1.0.Final/arquillian-config-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/config/arquillian-config-impl-base/1.1.0.Final/arquillian-config-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-test-impl-base/1.1.0.Final/arquillian-container-test-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-impl-base/1.1.2/shrinkwrap-impl-base-1.1.2.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-spi/1.1.2/shrinkwrap-spi-1.1.2.jar
/Users/sven/.m2/repository/org/jboss/arquillian/protocol/arquillian-protocol-servlet/1.1.0.Final/arquillian-protocol-servlet-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-spi/1.1.0.Final/arquillian-container-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/descriptors/shrinkwrap-descriptors-api-base/2.0.0-alpha-3/shrinkwrap-descriptors-api-base-2.0.0-alpha-3.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/descriptors/shrinkwrap-descriptors-spi/2.0.0-alpha-3/shrinkwrap-descriptors-spi-2.0.0-alpha-3.jar
/Volumes/SVEN/dev/eclipse/jee-oxygen2/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/1714/0/.cp/
/Volumes/SVEN/dev/eclipse/jee-oxygen2/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/1713/0/.cp/
1
Ist the dot at the end of "primary." intentional?fhossfel
No. Thanks. Didn't resolve the problem though.Sven Erik van t Veer
What does the persistence.xml look like?fhossfel
And can you get the persistent.xml through System.out.println(getClass().getResource("/META-INF/persistence.xml"));fhossfel

1 Answers

2
votes

The problem is not with your persistence.xml file or location of it, rather that you don't have provider JAR on your classpath, so it is not be plugged in. You should add dependency through maven for your desired persistence provider : Hibernate for example.

<dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
</dependency>

You can use OpenJPA (reference implementation if you add Java EE provided dependency

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

Once you have provider implementation JAR on your classpath, it will be picked up by JPA automatically.