hibernate.cxf.xml 設定檔 解決 內容必須匹配 "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)"

來源:互聯網
上載者:User

標籤:

這兩天想看看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*)"

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.