Reputation: 5198
I finally got Maven and JPA working but now I am experiencing another error:
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:
<?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:
<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. 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. If any further code is needed, please tell me in comments.
Upvotes: 1
Views: 4684
Reputation: 1562
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. there was a more specific inner exception inside the generic "Invalid persistence.xml" one. 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 has them in the following format:
<property name="hibernate.show_sql" value="true" />
while in hibernate.cfg.xml they were like:
<property name="hibernate.show_sql">true</property>
for the samples of various persistence.xml files i referred to this link
Upvotes: 0
Reputation: 290
While trying openjpa and using the openjpa maven plugin, i had the same error. For me the problem was solved by changing the xml declaration from
<?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:
javax.persistence.PersistenceException - JPA+Hibernate
Hope this helps!
Regards, Stefan
Upvotes: 0
Reputation: 19002
After you create the JAR make sure that the persistence.xml was put in the right place (extract the contents). In a war archive it must be in WEB-INF/classes/META-INF/persistence.xml
. In a simple jar, it must be in META-INF/persistence.xml
.
Specifically in your maven project, it must be in:
/src/main/resources/MAIN-INF/persistence.xml
Upvotes: 1