JPA:尝试加载持久性单元时出错

时间:2015-10-24 12:55:27

标签: java tomcat jpa centos jersey

这是一个使用Restful(Jersey)和JPA2编写的Web应用程序。我创建了一个war文件,并试图在Tomcat 8下的CentOS上部署这场战争。 当我启动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,Ubuntu上的Jetty 9。我不确定,为什么我看到这个错误。 有人能告诉我在CentOS上的部署有什么问题。

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这可能是因为EclipseLink中尚未解决的错误。请参阅此处讨论的Eclipse Bugzilla Bug #353121Eclipse Forum

用户提到了一个有助于他的案例的解决方法:

  

顺便说一下,我找到了一个不需要修补代码的解决方法:我禁用了#34;服务模块而没有发布&#34; Eclipse中的Tomcat配置。