Java 初始化persistence.xml时出错

Java 初始化persistence.xml时出错,java,maven,jpa,persistence,tomcat8,Java,Maven,Jpa,Persistence,Tomcat8,我正在进行一个项目,项目包括: Web动态项目+JPA 2.1/EclipseLink 2.5.2 Tomcat 8.0.28/JRE 1.8.0_66 月蚀 当作为Java应用程序在一个简单的JPA项目中执行时,项目的JPA部分运行良好。 我没有对代码的那部分做任何更改 我想在JSP中打印一些请求结果。我生成了一个servlet,并试图打印数据库中已经创建的用户的名称。几个小时以来,我一直没有找到关于JDBC驱动程序和persistence.xml的错误 为了解决依赖性问题,我添加了Maven

我正在进行一个项目,项目包括: Web动态项目+JPA 2.1/EclipseLink 2.5.2 Tomcat 8.0.28/JRE 1.8.0_66 月蚀

当作为Java应用程序在一个简单的JPA项目中执行时,项目的JPA部分运行良好。 我没有对代码的那部分做任何更改

我想在JSP中打印一些请求结果。我生成了一个servlet,并试图打印数据库中已经创建的用户的名称。几个小时以来,我一直没有找到关于JDBC驱动程序和persistence.xml的错误

为了解决依赖性问题,我添加了Maven

现在,我的用户被打印在JSP中,这很好,但是我以前使用的“Run.java”类不再工作了

作为“Java应用程序”运行我的应用程序时出现的错误让我不知所措:

此处没有任何更改,persistence.xml中也没有更改

“唯一”的更改出现在build.path中,因为项目的性质发生了更改(JPA->WebDynamic+JPA)。Maven已经解决了Web服务器和mysql连接器的问题,但是现在应用程序端坏了

你看到我没有看到的东西了吗


Thanx.

在项目的构建路径中,我添加了另一个“JRE系统库”。 我以前有:JDK1.8.066 我补充道:JRE 1.8.066


现在JSP可以工作,java也可以运行类。

奇怪,我认为这是EclipseLink 2.6.1特有的问题。你说应用程序实际上完全出错了?升级到最近的EclipseLink可能值得一试。在最近的版本中,这已经最小化到只记录异常,没有崩溃。相关bug链接:好吧,我觉得这看起来很熟悉。显然,自从你上次迭代这个问题以来,事情又一次走下坡路了。是的,我记得你告诉我关于EclipseLink 2.6.1的事情,这就是为什么我强制执行2.5.2版本:)你看,我听你说^^^是的,我有一个Run.java类,以便轻松操作我的读写操作:添加用户,删除用户,等等。与DB的所有操作都由LayerDAO.class处理。LayerDAO工作得很好,因为它将我的用户数据打印到JSP页面中。对Run.java的相同处理导致了这个错误。好吧,我第一次遇到这个问题时,我正在尝试使用Glassfish。但我肯定没有成功地使用它。在项目的目标运行时,我将Glassfish更改为默认JRE(1.8.0_66),所有这些都开始工作。但是我需要一个网络服务器。所以这一次我尝试了Tomcat,正如你所说的,事情又走下坡路了。我尝试用2.6.1 EclipseLink版本更改2.5.2,它对我的JSP很有效。但是java运行类仍然存在错误。
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
<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>
<?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>
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();
    }