hibernate.cxf.xml 配置文件 解决 内容必须匹配 "(property*,mapping*,(classcachecollectioncache)*,event*,listener*)

来源:互联网 发布:阿里巴巴淘宝城 编辑:程序博客网 时间:2024/06/05 00:18

原网页地址:http://www.lxway.com/9144556.htm

这两天想看看hibernate的东西,就跟着官网教程自己做,被官网网页上的配置文件给坑了。

有两个注意的地方,如果是按照官网的筒子们注意啦,一个是hibernate的头xsd验证文件,不修改成dtd读取hibernate.cxf.xml会

抛出Could not parse configuration: /hibernate.cfg.xml或者org.hibernate.MappingException: invalid configuration异常的哦。

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

6 改成下面的格式

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

还有的同学会抛出 元素类型为 “session-factory” 的内容必须匹配 “(property*,mapping*,(class-cachecollection-cache),event,listener*)”。

这个异常的原因可能有两个原因造成:

1.hibernate中配置文件的顺序写的有错误,按顺序配置<property>,<mapping> , <event><listener>标签如下:

 注意property, mapping, event 和 listener 之间的顺序  <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property>  <mapping class="com.yami.hibernate.pojo.SingleUser" />  <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> <event></event> <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的格式有问题。

一直以为是自己的配置文件哪里写错了,怎么看都没有找到问题,到最后才发现是自己拷贝了官网页面上的配置文件的格式有问题,看来有时候还是不能太相信官网呀。

0 0
原创粉丝点击