hibernatepersistence.xmlpersistence-unitjpa-2.2

Hibernate : Error in persistence.xml - Element 'persistence-unit' cannot contain text content. The content type is defined as element-only


I'm using Hibernate and the below persistence.xml was working fine. But after creating a new JPA entity from Table in eclipse, persistence.xml is giving error - Element 'persistence-unit' cannot contain text content. The content type is defined as element-only. I have removed the new class from persistence.xml, but still the error persists.

<persistence version="2.2"
    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_2.xsd">
    <persistence-unit name="FirstJPAProject">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class>com.boi.network.test.entity.Location</class>
        <class>com.boi.network.test.entity.Supportvendor</class>
        <class>com.boi.network.test.entity.Project</class>
        <properties>
            <!-- The JDBC URL to the database instance -->
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/boi?useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC"></property>          
            <property name="javax.persistence.jdbc.user" value="dba"></property>
            <property name="javax.persistence.jdbc.password" value="12345"></property>
        </properties>
    </persistence-unit>
</persistence>```

Solution

  • Hibernate supports auto-detection, so just remove your class elements and add this property, Also annotate your classes with @Entity annotation.

    <persistence version="2.2"
        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_2.xsd">
        <persistence-unit name="FirstJPAProject">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <!--<class>com.boi.network.test.entity.Location</class>
            <class>com.boi.network.test.entity.Supportvendor</class>
            <class>com.boi.network.test.entity.Project</class>-->
            <properties>
                <!-- The JDBC URL to the database instance -->
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/boi?useSSL=false&amp;useJDBCCompliantTimezoneShift=true&amp;useLegacyDatetimeCode=false&amp;serverTimezone=UTC"></property>          
                <property name="javax.persistence.jdbc.user" value="dba"></property>
                <property name="javax.persistence.jdbc.password" value="12345"></property>
                <property name="hibernate.archive.autodetection" value="class, hbm"/>
            </properties>
        </persistence-unit>
    </persistence>```
    

    Have a look to this answer