在将Jetty部署为WAR时,Hibernate / JPA persistence.xml会导致SAXParseException

时间:2011-09-27 13:32:07

标签: hibernate jpa maven jetty jpa-2.0

我无法在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

我得到了同样的错误。

我的应用正在使用的hibernate / 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时运行:运行而不是在部署为战争时运行。我已经尝试了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>

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

如果您正在使用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>