这是一个网络应用程序,使用 Restful (Jersey) 和 JPA2 编写。我创建了一个 war 文件,并试图在 Tomcat 8 下的 CentOS 上部署这场 war 。 当我启动 tomcat 时,tomcat 启动成功。但是当我尝试访问 URL 时,我看到以下错误。
Exception Description: An exception was thrown while trying to load persistence unit at url: file:/opt/tomcat/webapps/DSystem.war
Internal Exception: Exception [EclipseLink-30004] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while processing persistence.xml from URL: file:/opt/tomcat/webapps/DSystem.war
Internal Exception: java.net.MalformedURLException
这是 Maven pom.xml 文件。
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.d.dsystem</groupId>
<artifactId>DSystem</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>DSystem</name>
<build>
<finalName>DSystem</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.0</version>
</dependency>
<!--Externally adding at jar file for Maven-->
<dependency>
<groupId>org.mysql</groupId>
<artifactId>mysql</artifactId>
<scope>system</scope>
<version>3.0.11</version>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/mysql-connector-java-3.0.11-stable-bin.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<scope>system</scope>
<version>1.2.8</version>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/log4j-1.2.8.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-apache-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-mustache</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.22</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
persistence.xml 文件位于 src/main/java/META-INF 位置。这是我的 persistence.xml 文件有
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="DSystemService" transaction-type="RESOURCE_LOCAL">
<class>com.d.dsystem.db.Trans</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://localhost:3306/dsystemdb"/>
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
</properties>
</persistence-unit>
</persistence>
这是我的代码:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("DSystemService");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.persist(transObj);
entityManager.getTransaction().commit();
相同的 war 文件在 tomcat 8 windows 上工作,Jetty 9 在 Ubuntu 上工作。我不确定,为什么我会看到这个错误。 谁能告诉我 CentOS 上的部署有什么问题。
感谢任何帮助。
最佳答案
这可能是因为 EclipseLink 中一个尚未解决的错误。参见 Eclipse Bugzilla Bug #353121也在这里讨论:Eclipse Forum
用户提到了对他的情况有帮助的解决方法:
By the way, I found a workaround that does not require patching of code : I disabled the "Serve modules without publishing" of the Tomcat configuration in Eclipse.
关于java - JPA:尝试加载持久性单元时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318462/