gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:39:22 26 4
gpt4 key购买 nike

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

我在服务器上有一个错误 500:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named 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

非常感谢

最佳答案

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

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

关于tomcat - 部署到tomcat服务器时出现JPA错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54115254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com