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

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

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

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

1 <hibernate-configuration
2         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 PUBLIC
8       "-//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的格式有问题。

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

 

posted @ 2015-04-21 23:27  断点.  阅读(1218)  评论(0编辑  收藏  举报