2015-09-10 63 views
1

我在配置MySQL的hibernate 5.0.1時出現問題,我在這裏看到一些問題,說明版本4中有一個錯誤,我不知道天氣是相同的錯誤還是我做錯了什麼。在配置hibernate 5.0.1和MySQL時出錯

這裏是我的配置文件 的hibernate.cfg.xml

<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">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> 
    <property name="connection.username">root</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.MySQLDialect</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.cypher.dto.UserDetails"/> 
    </session-factory> 
</hibernate-configuration> 

這裏是我的SessionFactory的代碼

SessionFactory sessionfactory = new Configuration().configure().buildSessionFactory(); 
Session session = sessionfactory.openSession(); 
session.beginTransaction(); 
session.save(user); 
session.getTransaction().commit(); 

以下是錯誤日誌。

Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Unable to perform unmarshalling at line number 12 and column 63 in RESOURCE hibernate.cfg.xml. Message: cvc-elt.1: Cannot find the declaration of element 'hibernate-configuration'. 
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:133) 
    at org.hibernate.boot.cfgxml.internal.JaxbCfgProcessor.unmarshal(JaxbCfgProcessor.java:65) 
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:55) 
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:259) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:245) 
    at org.cypher.hibernate.HibernateTest.main(HibernateTest.java:15) 
Caused by: javax.xml.bind.UnmarshalException 

謝謝。

+0

是否有您使用的hibernate.cfg.xml不僅persistence.xml中的一個原因?你使用Hibernate的propriety API? –

+0

@MikeArgyriou @MikeArgyriou是的,我使用休眠API –

回答

4

所以這裏是解決方案。

更換

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

<hibernate-configuration> 

,並在文件的頂部添加下面的代碼。

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

此外,在配置文件替換<mapping resource=...>

0

嘗試插入下面的hibernate.cfg.xml文件的開頭:

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

我試過這個解決方案,但它給我的hibernate配置聲明錯誤,說明必須爲元素類型「hibernate-configuration」聲明''xmlns'。' –

+0

嘗試替換標籤與以下內容:

+0

嘗試相同,仍然沒有運氣,相同的錯誤,屬性「xmlns」必須被聲明爲元素類型「hibernate-configuration」。 –