【问题标题】:Exception in thread "main" org.hibernate.MappingException: invalid configuration in hibernate4线程“主”org.hibernate.MappingException 中的异常:hibernate4 中的配置无效
【发布时间】:2014-04-22 13:17:12
【问题描述】:

我遇到了休眠配置文件异常,这是我的 hibernate.cfg.xml 文件:

<?xml version='1.0' encoding='utf-8'?>

<hibernate-configuration
        xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
        xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
    <property name="connection.username">postgresql</property>
    <property name="connection.password">password</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.PostgresPlusDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
    <mapping resource="org.person.PersonModel"/>
  </session-factory>
</hibernate-configuration>

我正在使用 hibernate-core-4.1.12.Final.jar,我收到以下异常:

INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Exception in thread "main" org.hibernate.MappingException: invalid configuration
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2022)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1939)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1918)
    at org.person.PersonPersistance.main(PersonPersistance.java:14)
Caused by: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 25; Document is invalid: no grammar found.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)

通过查看相应的异常,我知道我的配置文件中有一些错误。我想知道我在配置文件中犯了什么错误。

【问题讨论】:

  • 这个答案有帮助吗:stackoverflow.com/a/12294976/95033 ?
  • 感谢您的信息,我在修改 WARN: HHH000223: Recognized obsolete hibernate namespace hibernate.sourceforge.net 后得到了这个异常。请改用命名空间hibernate.org/dtd。请参阅 Hibernate 3.6 迁移指南!线程“主”org.hibernate.HibernateException 中的异常:无法解析配置:/hibernate.cfg.xml

标签: java jakarta-ee hibernate-4.x


【解决方案1】:

当我在 Eclipse Luno 中编写相同的程序时,它已被执行。试试这个看看。

&lt;hibernate-configuration&gt;标签之前添加这个:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

【讨论】:

  • 请提供更多信息,说明您认为这可以解决问题的原因。
【解决方案2】:

对于 Hibernate 4.x,您应该使用与 3.x 相同的 DTD:

<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 2016-06-09
    • 2016-03-01
    • 2017-06-03
    • 2023-03-19
    • 1970-01-01
    • 2020-01-03
    相关资源
    最近更新 更多