0

I'm working on a project made with : Web Dynamic Project + JPA 2.1 / EclipseLink 2.5.2 Tomcat 8.0.28 / JRE 1.8.0_66 Eclipse Luna

JPA part of the project was running fine when executed in a simple JPA project as a Java Application. I made no changes on that part of the code.

I wanted to print some request results in a JSP. I generated a servlet and tried to print the name of a user already created in the database. For hours I had errors about JDBC driver and persistence.xml not found.

In order to solve dependancies problems I added Maven.

Now, my user is printed in the JSP and that's fine, but my "Run.java" class I used previously doesn't work anymore.

I'm quiet lost with the error I get when running my app as a "Java Application" :

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.<clinit>(EntityManagerFactoryProvider.java:55)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:92)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:188)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at dao.LayerDAO.<init>(LayerDAO.java:16)
    at exec.Run.main(Run.java:18)
Caused by: java.lang.NullPointerException
    at org.eclipse.persistence.platform.server.NoServerPlatformDetector.checkPlatform(NoServerPlatformDetector.java:28)
    at org.eclipse.persistence.platform.server.ServerPlatformUtils.detectServerPlatform(ServerPlatformUtils.java:58)
    at org.eclipse.persistence.internal.jpa.IsolatedHashMap.<clinit>(IsolatedHashMap.java:48)
    ... 7 more

Clearly the error occurs when I'm creating the EntityManagerFactory in the DAO class (the same one that is working with the JSP).

Here is my POM :

<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>CitizenWeb</groupId>
    <artifactId>com.citizenweb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <resources>
            <resource>
                <directory>src</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <properties>

        <!-- Generic properties -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- Web -->
        <jsp.version>2.2</jsp.version>
        <jstl.version>1.2</jstl.version>
        <servlet.version>2.5</servlet.version>

        <!-- Logging -->
        <logback.version>1.0.13</logback.version>
        <slf4j.version>1.7.5</slf4j.version>

        <!-- Test -->
        <junit.version>4.11</junit.version>

    </properties>
    <dependencies>

        <!-- Other Web dependencies -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>${jsp.version}</version>
            <scope>provided</scope>
        </dependency>

        <!-- Logging with SLF4J & LogBack -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>
        <!-- EclipseLink -->
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.core</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.5.2</version>
        </dependency>
        <!-- MySQL DB -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>

    </dependencies>
</project>

If needed, here is my persistence.xml :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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">
    <persistence-unit name="CitizenWeb" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <mapping-file>META-INF/orm.xml</mapping-file>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/citizen"/>
            <property name="javax.persistence.jdbc.user" value="user"/>
            <property name="javax.persistence.jdbc.password" value="user"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
        </properties>
    </persistence-unit>
</persistence>

Entities mapping is managed with an orm.xml

Here's also the beginning of the DAO class :

package dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;

import entities.Address;
import entities.GroupAssoc;
import entities.RightsGranted;
import entities.User;

public class LayerDAO implements IFLayerDAOLocal {

    EntityManager em = Persistence.createEntityManagerFactory("CitizenWeb").createEntityManager();

    public void createUser(User user) {
        System.out.println("DAO > addUser > "+em.hashCode());
        em.getTransaction().begin();
        em.persist(user);
        em.getTransaction().commit();
    }

Nothing has been changed here, neither in the persistence.xml

The "only" changes are in the build.path because of the change in the nature of project (JPA -> Web Dynamic+JPA). Maven has solved the problem with the webserver and the mysql connector, but now the app side is broken.

Do you see something I don't ?

Thanx.

Lovegiver
  • 413
  • 6
  • 22
  • Weird, I thought this was a problem specific to EclipseLink 2.6.1. And you say that the application actually errors out completely? Upgrading to the most recent EclipseLink might be worth a shot. In more recent versions this has minimized to the exception only being logged, nothing crashes. Relevant bug link: https://bugs.eclipse.org/bugs/show_bug.cgi?id=463629 – Gimby Dec 08 '15 at 21:33
  • Okay I thought this looked familiar. http://stackoverflow.com/questions/34085811/eclipse-doesnt-initialize-jpa-persistence . Apparently things went downhill again since your last iteration of this problem. – Gimby Dec 08 '15 at 21:41
  • Yes, I remember what you told me about EclipseLink 2.6.1, that's why I forced 2.5.2 version :) You see, I listen to you ^^ Yes, I've got a Run.java class in order to manipulate easily my read and write actions : add User, remove them, etc. All operations with the DB are processed by the LayerDAO.class. The LayerDAO works fine because it prints my User data into the JSP page. The same treatments with the Run.java lead me to this error. – Lovegiver Dec 08 '15 at 21:45
  • Well, the first time I had this problem, I was trying to work with Glassfish. But I definitely didn't succeed in using it. In the targeted runtime of the project, I changed Glassfish for default JRE (1.8.0_66) and all started to work. But I need a web server. So this time I tried Tomcat and as you said, things went downhill again. I tried to change 2.5.2 with 2.6.1 EclipseLink version, it works well for my JSP. But the java Run class is still stucked with error. – Lovegiver Dec 08 '15 at 22:00

1 Answers1

0

In the buildpath of the project, I added an alternate "JRE System Library". I previously had : JDK 1.8.0_66 I added : JRE 1.8.0_66

Now JSP works and also java Run class.

Lovegiver
  • 413
  • 6
  • 22