標籤:
這兩天想看看hibernate的東西,就跟著官網教程自己做,被官網網頁上的設定檔給坑了。
有兩個注意的地方,如果是按照官網的筒子們注意啦,一個是hibernate的頭xsd驗證檔案,不修改成dtd讀取hibernate.cxf.xml會
拋出Could not parse configuration: /hibernate.cfg.xml或者org.hibernate.MappingException: invalid configuration異常的哦。
1 <hibernate-configuration2 xmlns="http://www.hibernate.org/xsd/hibernate-configuration"3 xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">5 6 改成下面的格式7 <!DOCTYPE hibernate-configuration PUBLIC8 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"9 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
還有的同學會拋出 元素類型為 "session-factory" 的內容必須匹配 "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)"。
這個異常的原因可能有兩個原因造成:
1.hibernate中設定檔的順序寫的有錯誤,按順序配置<property>,<mapping> , <event>和<listener>標籤如下:
1 注意property, mapping, event 和 listener 之間的順序 2 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 3 <property name="connection.url">jdbc:mysql://localhost/hibernate</property> 4 5 6 7 <mapping class="com.yami.hibernate.pojo.SingleUser" /> 8 <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> 9 10 11 <event></event>12 <listener></listener>
2.如果不是順序問題的話,就只有一個可能性了,你的設定檔可能是從網頁上或者其他通過格式化工具拷貝的,有些編碼沒有轉換,導致hibernate讀取設定檔也會拋這個錯誤,下面就給一個可以用的設定檔(算是福利嗎哈哈),
拷貝到自己的工程中再重新設定下就可以了:
<?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">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">erik</property> <property name="connection.password">test</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.CollectionCacheInvalidator </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">create</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Names the annotated entity class --> <mapping class="com.yami.hibernate.pojo.SingleUser" /> </session-factory></hibernate-configuration>
原以為hibernate官網上的例子應該沒問題的,結果搞起來問題不斷,太相信了官網上的代碼了,弄了好久,才發現是xml的格式有問題。
一直以為是自己的設定檔哪裡寫錯了,怎麼看都沒有找到問題,到最後才發現是自己拷貝了官網頁面上的設定檔的格式有問題,看來有時候還是不能太相信官網呀。
hibernate.cxf.xml 設定檔 解決 內容必須匹配 "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)"