JPA - 无效的persistence.xml

时间:2014-02-17 16:29:59

标签: java hibernate maven jpa persistence

我终于让Maven和JPA工作了,但现在我遇到了另一个错误:

javax.persistence.PersistenceException: Invalid persistence.xml.
Error parsing XML (line-1 : column -1): cvc-elt.1: Declaration of the element "persistence" could not be found.

    org.hibernate.ejb.packaging.PersistenceXmlLoader.loadURL(PersistenceXmlLoader.java:147)
    org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:171)
    org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:325)
    org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:71)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63)
    javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
    com.gamerbud.model.GameController.getEntityManager(GameController.java:31)
    com.gamerbud.model.GameController.getGames(GameController.java:37)
    com.gamerbud.SessionBean.updateGames(SessionBean.java:41)
    com.gamerbud.SessionBean.<init>(SessionBean.java:36)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    java.lang.Class.newInstance(Class.java:374)
    org.apache.myfaces.config.annotation.Tomcat7AnnotationLifecycleProvider.newInstance(Tomcat7AnnotationLifecycleProvider.java:60)
    org.apache.myfaces.config.ManagedBeanBuilder.buildManagedBean(ManagedBeanBuilder.java:156)
    org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.createManagedBean(ManagedBeanResolver.java:333)
    org.apache.myfaces.el.unified.resolver.ManagedBeanResolver.getValue(ManagedBeanResolver.java:296)
    javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
    org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:179)
    org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72)
    org.apache.el.parser.AstValue.getValue(AstValue.java:161)
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
    org.apache.myfaces.view.facelets.el.ELText$ELTextVariable.toString(ELText.java:208)
    org.apache.myfaces.view.facelets.el.ELText$ELTextComposite.toString(ELText.java:140)
    org.apache.myfaces.view.facelets.compiler.CommentInstruction.write(CommentInstruction.java:40)
    org.apache.myfaces.view.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:46)
    org.apache.myfaces.view.facelets.compiler.UILeaf.encodeAll(UILeaf.java:505)
    javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:541)
    javax.faces.component.UIComponentBase.encodeAll(UIComponentBase.java:541)
    org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1981)
    org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:285)
    org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:116)
    org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:241)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:199)

这对我来说很奇怪,因为我的persistence.xml defenitly有一个持久性元素:

<?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.hsn_gamerbud_war_1.0-SNAPSHOTPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.gamerbud.model.Game</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    </properties>
  </persistence-unit>
</persistence>

如果需要,这是我的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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hsn</groupId>
    <artifactId>gamerbud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>gamerbud</name>

     <properties>
        <!-- Netbeans specific hints -->
        <netbeans.hint.license>apache20</netbeans.hint.license>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

<dependencies>
        <!-- Apache MyFaces JSF implementation -->
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-api</artifactId>
            <version>2.1.11</version>
        </dependency>
        <dependency>
            <groupId>org.apache.myfaces.core</groupId>
            <artifactId>myfaces-impl</artifactId>
            <version>2.1.11</version>
            <scope>runtime</scope>
        </dependency>

        <!-- Servlet specific stuff is available on every container -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.2.0.Final</version>
        </dependency>

        <!-- JUNIT -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>eclipselink</artifactId>
      <version>2.5.0-SNAPSHOT</version>
    </dependency>
  <dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>javax.persistence</artifactId>
    <version>2.1.0-SNAPSHOT</version>
  </dependency>
    <dependency>
        <groupId>org.eclipse.persistence</groupId>
        <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
      <version>2.5.0-SNAPSHOT</version>
      <scope>provided</scope>
    </dependency>
</dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.1</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>6.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  <repositories>
    <repository>
      <url>http://download.eclipse.org/rt/eclipselink/maven.repo/</url>
      <id>eclipselink</id>
      <layout>default</layout>
      <name>Repository for library EclipseLink (JPA 2.1)</name>
    </repository>
  </repositories>
</project>

我也尝试使用Eclipse Link而不是Hibernate,但是得到了几乎相同的错误。 我也在使用MySQL,我真的很感激,如果有人会给它看看,因为我对所有这些东西都很新,并且没有真正的线索在哪里搜索错误。如果需要任何进一步的代码,请在评论中告诉我。

3 个答案:

答案 0 :(得分:1)

创建JAR后,请确保将persistence.xml放在正确的位置(提取内容)。在战争档案中,它必须在WEB-INF/classes/META-INF/persistence.xml。在一个简单的jar中,它必须在META-INF/persistence.xml

特别是在您的maven项目中,它必须位于:

/src/main/resources/MAIN-INF/persistence.xml

答案 1 :(得分:0)

尝试使用openjpa并使用openjpa maven插件时,我遇到了同样的错误。对我来说,问题是通过从

更改xml声明来解决的
<?xml version="1.0"?>
<persistence version="1.0>
   ...
</persistence>

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
    ...
</persistence>

请仔细检查这里提到的JPA版本:

javax.persistence.PersistenceException - JPA+Hibernate

希望这有帮助!

此致,Stefan

答案 2 :(得分:0)

在我的情况下发生了这个错误,因为我从hibernate.cfg.xml中错误地复制粘贴的属性,从本机Hibernate更改为Hibernate JPA。在通用的“Invalid persistence.xml”中有一个更具体的内部异常。这是我在检查其他答案并修正persistence.xml的位置和标记属性后做的最后一件事。

persistence.xml具有以下格式:

<property name="hibernate.show_sql" value="true" />

在hibernate.cfg.xml中它们就像:

<property name="hibernate.show_sql">true</property>

对于我引用此link

的各种persistence.xml文件的示例