【问题标题】:Hibernate Configuration Error (Cannot find declaration of element 'hibernate-configuration')休眠配置错误(找不到元素“休眠配置”的声明)
【发布时间】:2012-04-11 13:06:23
【问题描述】:

我正在尝试使用休眠建立与我的数据库的简单连接。这是我的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">user</property>
<property name="connection.password">pass</property>

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

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</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="com/mycomp/pro/model/elem/elem.hbm.xml"/>
</session-factory>
</hibernate-configuration>

我收到以下错误:

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 6 and column 26 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'.
...

似乎有点不合逻辑。由于我将“hibernate-configuration”作为 hibernate.cfg.xml 文件中的根元素。

我使用的是 Hibernate 4.1.1(只是提一下,因为我得到了一些提示,新的 hibernate 可能存在一些问题)

希望有人可以提供帮助,因为我是 Hibernate 的新手,而且现在我也没有从 Google 获得任何重大帮助。

【问题讨论】:

    标签: java hibernate configuration


    【解决方案1】:

    使用以下命令构建会话工厂 -

    new Configuration().configure().buildSessionFactory();
    

    至于 Hibernate.cfg 你可以试试下面的头文件。

    <hibernate-configuration 
     xmlns="http://www.hibernate.org/xsd/hibernate-configuration" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration 
     https://github.com/hibernate/hibernate-orm/raw/master/hibernate-core/src/main/resources/org/hibernate/hibernate-configuration-4.0.xsd">
    

    目前 Hibernate 4.1 似乎存在错误(不确定稳定性)。我在邮件列表中找到了解决方案,因此也请检查一下。希望这会有所帮助。

    http://www.mail-archive.com/hibernate-dev@lists.jboss.org/msg06937.html

    【讨论】:

    • 我已经做了,但我仍然得到一些错误
    【解决方案2】:

    将会话工厂创建为新的 AnnotationConfiguration ()。配置 ()。构建会话工厂(); 希望它能帮助你建立联系......

    【讨论】:

      【解决方案3】:

      试试这个代码。

      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
          Session session = sessionFactory.openSession();
          session.beginTransaction();
      

      将标题添加到您的 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>
              --------
          <hibernate-configuration>
      

      【讨论】:

        【解决方案4】:

        D:\hibernate-release-5.1.17.Final\project folder复制粘贴休眠配置xml文件>>>>>>>搜索hibernate.cfg.xml 并选择了正确的文件,其中包括创建、DOCTYPE 和映射...

        只需根据您的数据库和设置更改值。

        【讨论】:

          猜你喜欢
          • 2016-01-02
          • 1970-01-01
          • 2010-11-08
          • 2016-09-20
          • 1970-01-01
          • 1970-01-01
          • 2015-07-28
          • 2016-05-06
          • 2018-10-21
          相关资源
          最近更新 更多