2012-03-27 90 views
4

我試圖建立一個簡單的連接到我的數據庫使用休眠。下面是我的配置文件:Hibernate配置錯誤(無法找到'hibernate-configuration'元素聲明)

<?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和右現在我還沒有從谷歌任何重要的幫助。

回答

2

使用下面的命令建立會話工廠 -

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似乎正在遭受bug(不確定穩定性)。我在郵件列表中找到了解決方案,請檢查一下。希望這可以幫助。

http://www.mail-archive.com/[email protected]/msg06937.html

0

將會話工廠創建爲新的AnnotationConfiguration()。配置 ()。 buildSessionFactory(); 希望它會幫助你建立連接...

0

試試這個代碼。

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> 
相關問題