Hibernate配置错误(找不到元素'hibernate-configuration'的声明) [英] Hibernate Configuration Error (Cannot find declaration of element 'hibernate-configuration')

查看:1106
本文介绍了Hibernate配置错误(找不到元素'hibernate-configuration'的声明)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用hibernate建立一个到我的数据库的简单连接。这是我的配置文件:

 <?xml version =1.0encoding =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>
<! - 数据库连接设置 - >
< 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连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>

<! - - SQL方言 - >
< property name =dialect> org.hibernate.dialect.HSQLDialect< / property>

<! - 启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / property>

<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.internal.NoCacheProvider< / property>

<! - 将所有执行的SQL回复到stdout - >
< property name =show_sql> true< / property>

<! - 在启动时删除并重新创建数据库模式 - >
< property name =hbm2ddl.auto>更新< / property>
< mapping resource =com / mycomp / pro / model / elem / elem.hbm.xml/>
< / session-factory>
< / hibernate-configuration>

我得到以下错误:

 线程main中的异常org.hibernate.internal.util.config.ConfigurationException:无法在RESOURCE hibernate.cfg.xml的第6行和第26列执行反编组。消息:cvc-elt.1:找不到元素'hibernate-configuration'的声明。 
...

看起来有点不合逻辑。因为我在hibernate.cfg.xml文件中有'hibernate-configuration'作为根元素。



我使用Hibernate 4.1.1(仅仅提到,因为我已经得到了一些提示,说明新的hibernate可能会有一些问题)

希望有人能帮助,因为我是Hibernate的新手,现在我没有得到任何东西主要帮助来自谷歌。

解决方案

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

  new Configuration()。configure()。buildSessionFactory(); 

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

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

目前Hibernate 4.1似乎正在遭受bug(不确定稳定性)。我在邮件列表中找到了解决方案,请检查一下。希望这有帮助。


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



I'm trying to establish a simple connection to my database using hibernate. Here is my configuration file:

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

I get the following error:

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'.
...

Seems bit illogical. Since I have 'hibernate-configuration' as the root element in the hibernate.cfg.xml file.

I'm using Hibernate 4.1.1 (just mentioning, since I've gotten some hints that the new hibernate could possibly have some issues)

Hopefully somebody can help since I'm new to Hibernate and right now I'm not getting any major help from google either.

解决方案

Build the Session Factory using the below command -

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

As for the Hibernate.cfg you could try the below header.

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

Currently Hibernate 4.1 seems to be suffering from bugs(Not sure about stability). I found the solution at a mailing list so check that out as well. Hope this helps.

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

这篇关于Hibernate配置错误(找不到元素'hibernate-configuration'的声明)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆