部署到tomcat服务器时出现JPA错误

时间:2019-01-09 17:14:49

标签: tomcat jpa java-ee netbeans glassfish

我在netbeans上有一个JavaEE应用程序(webapp)(在glassfish服务器上运行)。 我使用JPA ORM。 我在MAVEN上导入了所有正确的jar。 当我想在远程服务器上运行此Web应用程序的WAR文件(在tomcat上运行)时,JPA使其崩溃...

服务器上出现错误500:

  

javax.persistence.PersistenceException:没有针对以下对象的持久性提供程序   EntityManager命名   com.mycompany_EmployeesManagementApplication2_war_1.0-SNAPSHOTPU     javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)     javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)     models.DataTransaction。(DataTransaction.java:31)     controllers.SingleController.doGet(SingleController.java:37)     javax.servlet.http.HttpServlet.service(HttpServlet.java:635)     javax.servlet.http.HttpServlet.service(HttpServlet.java:742)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

这是我的pom.xml:

http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0

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

<name>EmployeesManagementApplication2</name>

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

<dependencies>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa</artifactId>
        <version>2.5.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>javax.persistence-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </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>

这是我的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="com.mycompany_EmployeesManagementApplication2_war_1.0-SNAPSHOTPU" transaction-type="JTA">
    <jta-data-source>java:app/awsrds</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="create"/>
    </properties>
  </persistence-unit>
</persistence>

我的持久性文件正确位于ressources / META-INF

非常感谢您

1 个答案:

答案 0 :(得分:2)

原因是Tomcat不是完整的Java EE服务器,它只是servlet容器,不提供Glassfish所提供的JPA。如果希望将JPA与Tomcat一起使用,则需要将其作为库包含在WAR中。

如果您使用的是纯JPA而不是任何Eclipselink功能,则可以使用包含Apache OpenJPA的TomEE作为替代JPA提供程序。另外,您可以继续使用包含Eclipselink的GlassFish或Payara,而无需包含它。