Search code examples
javahibernatehibernate-annotations

Java Hibernate invalid mapping exception


My code was working just fine with the xml mapping then I changed to annotations and i get this error:

Exception in thread "main" org.hibernate.InvalidMappingException: Unable to read XML

This is my XML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost/hibernate1?useLegacyDatetimeCode=false&amp;serverTimezone=UTC</property>
        <property name="connection.username">hibernate1</property>
        <property name="connection.password">mypassword</property>
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.show_sql">true</property>
        <mapping class="hibernate.Profesor" file="" jar="" package="" resource=""/>
        <mapping class="hibernate.Direccion" file="" jar="" package="" resource=""/>
    </session-factory>
</hibernate-configuration>

Solution

  • I was able to get it to work for me changing only connection.url, connection.username, and connection.password with:

    // A SessionFactory is set up once for an application
    SessionFactory sessionFactory = new Configuration()
        .configure() // configures settings from hibernate.cfg.xml
        .buildSessionFactory();
    

    Delete your mapping configuration files. That is, delete Profesor.hbm.xml and Direccion.hbm.xml if they still exist. You don't need them if you are using annotations. If you are using Maven, make sure you have the following dependencies:

    Also, com.mysql.jdbc.Driver is deprecated. Use com.mysql.cj.jdbc.Driver.

    It would be more helpful if you posted the exception's entire stack trace.