简体   繁体   English

JPA-无效的persistence.xml

[英]JPA - Invalid persistence.xml

I finally got Maven and JPA working but now I am experiencing another error: 我终于使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)

Which is strange to me since my persistence.xml defenitly has an persistence element: 这对我来说很奇怪,因为我的persistence.xml无疑具有一个持久性元素:

<?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>

Here is my pom.xml if needed: 如果需要,这是我的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>

I also tried to use Eclipse Link instead of Hibernate, but got pretty much the same error. 我也尝试使用Eclipse Link代替Hibernate,但是得到了几乎相同的错误。 Also I am using MySQL, I really would appreciate if somebody would give it at look, since I am very new to all this stuff and have no real clue where to search for the error. 另外,我正在使用MySQL,如果有人看过它,我将不胜感激,因为我对所有这些东西都很陌生,并且不知道在哪里搜索错误。 If any further code is needed, please tell me in comments. 如果需要任何其他代码,请在评论中告诉我。

After you create the JAR make sure that the persistence.xml was put in the right place (extract the contents). 创建JAR之后,请确保将persistence.xml放在正确的位置(提取内容)。 In a war archive it must be in WEB-INF/classes/META-INF/persistence.xml . 在战争档案中,它必须位于WEB-INF/classes/META-INF/persistence.xml In a simple jar, it must be in META-INF/persistence.xml . 在一个简单的jar中,它必须位于META-INF/persistence.xml

Specifically in your maven project, it must be in: 特别是在您的Maven项目中,它必须位于:

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

While trying openjpa and using the openjpa maven plugin, i had the same error. 在尝试使用openjpa和使用openjpa maven插件时,我遇到了相同的错误。 For me the problem was solved by changing the xml declaration from 对我来说,这个问题是通过将xml声明从

<?xml version="1.0"?>
<persistence version="1.0>
   ...
</persistence>

to

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

And please double check the JPA version as mentioned here: 并且请仔细检查JPA版本,如下所示:

javax.persistence.PersistenceException - JPA+Hibernate javax.persistence.PersistenceException-JPA + Hibernate

Hope this helps! 希望这可以帮助!

Regards, Stefan 问候,斯蒂芬

in my case this error was happening because i incorrectly copy-pasted properties from hibernate.cfg.xml when changing from native Hibernate to Hibernate JPA. 在我的情况下,发生此错误是因为从本地Hibernate更改为Hibernate JPA时,我从hibernate.cfg.xml中错误地复制粘贴了属性。 there was a more specific inner exception inside the generic "Invalid persistence.xml" one. 通用“ Invalid persistence.xml”中有一个更具体的内部异常。 this was the last thing i did after checking the other answers here and correcting both the location of persistence.xml and the attributes of the tag. 这是我在这里检查其他答案并更正persistence.xml的位置和标记属性后所做的最后一件事。

persistence.xml has them in the following format: persistence.xml具有以下格式的它们:

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

while in hibernate.cfg.xml they were like: 而在hibernate.cfg.xml中,它们就像:

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

for the samples of various persistence.xml files i referred to this link 对于各种persistence.xml文件的示例,我引用了此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM