0

I'm new to Java EE 7. And I'm now stuck with making a Persistence connection.

It turned out that the error I'm running is common. I have tried the suggested solutions. But apparently I'm still doing something wrong. Hopefully someone will see the mistake I made.

Glassfish 4.1, Maven, Mysql 5.1.44 and I use Netbeans 8.2.

When I run the Test class. I get the following Exception:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named Test6_MYSQL
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at com.mycompany.simpleform5.Test.main(Test.java:25)

I have tried different solutions suggested in:

I have implemented the instructions explained in: No Persistence provider for EntityManager named --> I’ve changed the "hibernate-entitymanager.jar" for a newer version: "hibernate-core.jar".

I have also tried other the older Persistence versions 1.0 and 2.0.

I have changed the org.hibernate.ejb.HibernatePersistence: (https://stackoverflow.com/a/1285436/784594).

I used the hibernate-core-5.2.2.Final.jar (Hibernate 5.2.2: No Persistence provider for EntityManager). But that didn't work.

The java class:

public class Test 
{
    public static void main (String[] args)
    {
        [..]
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test6_MYSQL");
        EntityManager em = emf.createEntityManager();
        [..]

    }

}

META-INF/Persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="Test6_MYSQL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.mycompany.simpleform6</class>
      <properties>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/so_database" />
      <property name="javax.persistence.jdbc.user" value="root" />
      <property name="javax.persistence.jdbc.password" value="admin" />
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="update" />
    </properties>
    </persistence-unit>
</persistence>

The pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>SimpleForm6</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>SimpleForm6</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <version>5.1.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.10.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.1.Final</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>unknown-jars-temp-repo</id>
            <name>A temporary repository created by NetBeans for libraries and jars it could not identify. Please replace the dependencies in this repository with correct ones and delete this repository.</name>
            <url>file:${project.basedir}/lib</url>
        </repository>
    </repositories>
</project>
halfer
  • 19,824
  • 17
  • 99
  • 186
Stefan H.
  • 11
  • 2
  • I know it's a silly question, but are you sure that META-INF/Persistence.xml is in the classpath ? You are clearly running it as a test class, how to you launch such class ? – gtosto Sep 12 '17 at 12:39
  • What you are trying to do makes no sense and cannot work. A webapplication running in Glassfish and a java application with a main method like your test are two completly different things... – unwichtich Sep 12 '17 at 12:43
  • I think that the META-INF/Persistence.xml is in the correct class path, for two reasons 1) I made the persistence unit with Netbeans , Netbeans places it in the correct default location I assume. 2) I made another working app with a DB (with the help of the automated generaton functions). The persitence.xml is in the same classpath. I launch the class with the 'run command' in Netbeans 8.2. I only use the test class for testing the persistence connection. In the test class I try to push data to the DB (I left this out of the explanation since I couldn’t made a persistence connection). – Stefan H. Sep 12 '17 at 14:08
  • @StefanH. Did you ever find a solution to this? I am having the exact same problem and can't find a solution – quantumbutterfly Apr 24 '18 at 19:24

0 Answers0