【问题标题】:Hibernate/JPA persistence.xml leads to SAXParseException when deploying to Jetty as WAR当作为 WAR 部署到 Jetty 时,Hibernate/JPA persistence.xml 会导致 SAXParseException
【发布时间】:2011-11-26 01:38:15
【问题描述】:

我无法让应用程序在 Jetty 上运行。

如果我使用 maven jetty 插件运行应用程序,即

mvn jetty:run

一切运行良好。如果我将应用程序打包为战争并尝试手动部署到 Jetty 服务器,则会出现异常

org.xml.sax.SAXParseException: cvc-complex-type.3.1: Value '2.0' of attribute 'version' of element 'persistence' is not valid with respect to the corresponding attribute use. Attribute 'version' has a fixed value of '1.0'.

我尝试使用它部署到 Jetty

mvn jetty:run-war

我得到同样的错误。

我的应用正在使用的休眠/JPA 依赖项是

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.6.7.Final</version>
        <exclusions>
            <exclusion>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
            </exclusion>
            <exclusion>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>4.1.0.Final</version>
        <exclusions>
            <exclusion>
                <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
    </dependency>

我很困惑为什么它在使用 jetty:run 运行时有效,而不是在作为战争部署时有效。我尝试了 Jetty 7 和 8 的各种版本,但都没有成功。

谢谢。

谁能发现我正在尝试的东西有什么问题?

编辑:继承persistence.xml

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/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">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>            
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>            
        </properties>
    </persistence-unit>
</persistence>

【问题讨论】:

    标签: hibernate jpa maven jetty jpa-2.0


    【解决方案1】:

    在 2.0 兼容库之前,您的类路径中是否有较旧的(在 JPA2 兼容之前)版本的休眠?它也可以通过一些依赖来实现。

    【讨论】:

    • 我看不到。我已经搜索了 WAR 的 WEB-INF/libs 文件夹中的每个 JAR 文件,并且对任何 JPA 1 的唯一引用是 hibernate-entitymanager-3.6.3.Final 中的文件 org/hibernate/ejb/persistence_1_0.xsd。不应该被用作persistence.xml的jar正在引用persistence_2_0.xsd
    • 对不起,我的错误。事实证明,jersey-spring maven 依赖引入了导致问题的 Spring 2.5 东西。
    【解决方案2】:

    如果您使用的是 maven 和 open-jpa maven 插件,请注意输入正确的版本

    如果你使用这个

                <plugin>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <includes>**/entities/*.class,**/entities/masterdata/*.class</includes>
                    <excludes>**/entities/*Enum.class</excludes>
                    <addDefaultConstructor>true</addDefaultConstructor>
    
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                </configuration>
                <executions>
                    <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.openjpa</groupId>
                        <artifactId>openjpa</artifactId>
                        <!-- set the version to be the same as the level in your runtime -->
                        <version>1.2.0</version>
                    </dependency>
                </dependencies>
    

    这会导致版本错误。应该是:

                <plugin>
                <groupId>org.apache.openjpa</groupId>
                <artifactId>openjpa-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <includes>**/entities/*.class,**/entities/masterdata/*.class</includes>
                    <excludes>**/entities/*Enum.class</excludes>
                    <addDefaultConstructor>true</addDefaultConstructor>
    
                    <enforcePropertyRestrictions>true</enforcePropertyRestrictions>
                </configuration>
                <executions>
                    <execution>
                        <id>enhancer</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>enhance</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.openjpa</groupId>
                        <artifactId>openjpa</artifactId>
                        <!-- set the version to be the same as the level in your runtime -->
                        <version>2.2.0*</version>
                    </dependency>
                </dependencies>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-08
      • 1970-01-01
      • 2012-12-31
      相关资源
      最近更新 更多