简体   繁体   English

Java Hibernate无效映射异常

[英]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: 我的代码与xml映射一起正常工作,然后更改为批注,但出现此错误:

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

This is my XML 这是我的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>

I was able to get it to work for me changing only connection.url , connection.username , and connection.password with: 我能够通过以下方式仅对connection.urlconnection.usernameconnection.password进行更改以使其工作:

// 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. 也就是说,删除Profesor.hbm.xmlDireccion.hbm.xml如果仍然存在)。 You don't need them if you are using annotations. 如果您使用注释,则不需要它们。 If you are using Maven, make sure you have the following dependencies: 如果使用的是Maven,请确保您具有以下依赖项:

Also, com.mysql.jdbc.Driver is deprecated. 另外,不建议使用com.mysql.jdbc.Driver Use com.mysql.cj.jdbc.Driver . 使用com.mysql.cj.jdbc.Driver

It would be more helpful if you posted the exception's entire stack trace. 如果您发布了异常的整个堆栈跟踪,将会更有帮助。

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

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