2012-09-05 44 views
9

當我使用下面的代碼實例化休眠:休眠:文件是無效的:沒有語法發現

 Configuration configuration = new Configuration(); 
     configuration.configure(); 
     serviceRegistry = new ServiceRegistryBuilder().applySettings(
       configuration.getProperties()).buildServiceRegistry(); 
     sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

我得到了以下情況除外:

org.xml.sax.SAXParseException:文件無效:找不到語法。, org.xml.sax.SAXParseException:文檔根元素 「hibernate-configuration」必須匹配DOCTYPE根「null」。 所致:org.hibernate.MappingException:無效配置 在org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014) 在org.hibernate.cfg.Configuration.configure(Configuration.java:1931) 在org.hibernate.cfg.Configuration.configure(Configuration.java:1910) at com.soccer.system.HibernateUtil。(HibernateUtil.java:23) ... 1 more 引起自:org.xml.sax.SAXParseException :文檔無效:找不到語法。

我用下面的hibernate.cfg.xml文件與Hibernate 4.1.6:

<?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.apache.derby.jdbc.ClientDriver</property> 
     <property name="connection.url">jdbc:derby://localhost:1527/K:\db</property> 
     <property name="connection.username"></property> 
     <property name="connection.password"></property> 

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

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.DerbyDialect</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> 
    </session-factory> 
</hibernate-configuration> 

我能做些什麼來消除這種異常?

回答

13

添加行

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

爲第二行(XML版本之後和<hibernate-configuration>標記之前,那麼它應該工作。

+5

添加'DOCTYPE'是不夠的,我也不得不從'冬眠,configuration'元素中刪除所有屬性。覺得奇怪的是有利於休眠'DTD'了'XSD'。ÿ你也可以更新'DTD' url來使用'「http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd」'而不是舊的sourceforge URL。 – Sydney

1

刪除此代碼和問題將resloved。

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"