Unexpected Element in Persistence.xml Entitymanager inject not working

1.2k Views Asked by At

My program was working perfectly fine, till I started to use persistence and with the EntityManager @Inject.

There are a lot of errors thrown, but I think this one is the one most blocking.

It says something about an unexpected element, but I can't find anything useful online about it.

Log:

Connected to server
[2017-05-16 09:13:17,808] Artifact web-example:war exploded: Artifact is being deployed, please wait...
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomcatWebAppBuilder deployWebApps
INFO: using default host: localhost
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomcatWebAppBuilder init
INFO: ------------------------- localhost -> /
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomEEClassLoaderEnricher validateJarFile
WARNING: jar '/Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT/WEB-INF/lib/hibernate-jpa-2.0-api-1.0.1.Final.jar' contains offending class: javax.persistence.Entity. It will be ignored.
mei 16, 2017 9:13:18 PM org.apache.tomee.catalina.TomEEClassLoaderEnricher validateJarFile
WARNING: jar '/Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT/WEB-INF/lib/jboss-transaction-api_1.1_spec-1.0.1.Final.jar' contains offending class: javax.transaction.Transaction. It will be ignored.
mei 16, 2017 9:13:18 PM org.apache.openejb.config.ConfigurationFactory configureApplication
INFO: Configuring enterprise application: /Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT
mei 16, 2017 9:13:18 PM org.apache.openejb.config.ReadDescriptors deploy
SEVERE: Unable to load Persistence Unit from EAR: /Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT, module: file:/Users/renzo/development/school/dea/spotitube/app/target/web-example-1.0-SNAPSHOT/WEB-INF/classes/. Exception: unexpected element (uri:"http://xmlns.jcp.org/xml/ns/persistence", local:"persistence"). Expected elements are <{http://java.sun.com/xml/ns/persistence}persistence>
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://xmlns.jcp.org/xml/ns/persistence", local:"persistence"). Expected elements are <{http://java.sun.com/xml/ns/persistence}persistence>

/resources/META-INF/persistence.xml

<persistence 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
http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit
            name="mysql"
            transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>
            nl.han.dea.domain.User
        </class>

        <properties>
            <property name="hibernate.archive.autodetection" value="class"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
            <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
            <property name="hibernate.connection.url"
                      value="jdbc:mysql://localhost:3306/Spotitube?autoReconnect=true&amp;useSSL=false&amp;serverTimezone=UTC"/>
            <property name="hibernate.connection.username" value="spotitube"/>
            <property name="hibernate.connection.password" value="java"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.flushMode" value="FLUSH_AUTO"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
        </properties>

    </persistence-unit>
</persistence>

pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>nl.han.dea</groupId>
    <artifactId>web-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>web-example</name>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
            <scope>compile</scope>
        </dependency>

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

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <failOnMissingWebXml>false</failOnMissingWebXml>
    </properties>
</project>
1

There are 1 best solutions below

0
On

The problem becomes apparent in this line of the log output:

Exception: unexpected element (uri:"http://xmlns.jcp.org/xml/ns/persistence", local:"persistence").

Expected elements are <{http://java.sun.com/xml/ns/persistence}persistence

This tells us, that their is a mismatch with JPA versions 2.0 and 2.1. The reason for this conflict lies in the rather old version of Hibernate that you refer to in the pom.xml presented.

Hibernate 4.2.x only supports JPA 2.0, whereas TomEE 7.0.x expects a JPA 2.1 provider as you told it so with a persistence.xml as presented in your question:

persistence 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 http://xmlns.jcp.org/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd" version="2.1">

For further details on JPA support of Hibernate, see Hibernate ORM download page on which these information is listed.

Solution

Most likely, changing Hibernate's version to 4.3.x should cure some of the problems you encounter. Here is how the relevant dependency declaration should look like:

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

In case you want to modernize your application further, give Hibernate 5.2.x a try. For the current version, the dependency declaration looks like:

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

Hope it helps.

Add-On

If you want to go for Hibernate 5.2.x in a TomEE >=7.0.3 setup, you are advised to add

<property name="tomee.jpa.cdi" value="false" />

to your persistence.xml, as otherwise the container will complain on injection issues on startup. For details, see this post and also here.

Moreover, change the <provider> entry to the new/correct package like this:

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>