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的格式有问题。

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

时间: 2024-08-26 19:52:25

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

元素类型为 &quot;session-factory&quot; 的内容必须匹配 &quot;(property*,mapping*,(class-cach....解决方法

http://www.cnblogs.com/kisso143/p/3642057.html property必须写在mapping的上面.

hibernate.cfg.xml配置文件

<span style="font-size:18px;"><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration

hibernate.cfg.xml配置文件和hbm.xml配置文件 模板

hibernate.cfg.xml配置文件格式 <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration

【Hibernate】Hibernate.cfg.xml配置文件详解

Hibernate配置文件主要用于配置数据库连接和Hibernate运行时所需的各种属性,这个配置文件应该位于应用程序或Web程序的类文件夹 classes中.Hibernate配置文件支持两种形式,一种是xml格式的配置文件,另一种是Java属性文件格式的配置文件,采用"键=值"的形式.建议采用xml格式的配置文件.xml配置文件可以直接对映射文件进行配置,并由Hibernate自动加载,而properties文件则必须在程序中通过编码加载映射文件. 注意:hibernate的前缀可

Hibernate之通过hibernate.cfg.xml配置文件访问数据库的例子

hibernate.cfg.xml文件内容: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0

hibernate.cfg.xml配置文件分析

!--标准的XML文件的起始行,version='1.0'表明XML的版本,encoding='gb2312'表明XML文件的编码方式-->                 <?xml version='1.0' encoding='gb2312'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性.hibernate.sourceforge.NET

hibernate.cfg.xml配置文件和hbm.xml配置文件

http://blog.sina.com.cn/s/blog_a7b8ab2801014m0e.html hibernate.cfg.xml配置文件格式 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  "

hibernate通过xml配置文件实现表与实体的映射

这里讨论的是一对多的关系 在做公交卡系统,会涉及到两张表,忽略两种表的作用,只关心他们之间的关系 : 卡规格表和卡类表,一种卡规格会对应多个卡类 实体类: /** * 卡类型表的实体 */ public class Acardtype implements java.io.Serializable { private Long id; private Cardstandard cardstandard; //卡规格表外键 ......... /** *卡规格表实体 */ public class

Hibernate框架hibernate.cfg.xml配置文件,配置自动生成表结构策略。

<property name="hbm2ddl.auto"></property> key -- hbm2ddl.auto:自动生成表结构策略 value -- update(使用最多):当数据库不存在表时,hibernate启动后会自动生成表结构. 当数据库表存在时,如果一样,则只会写入数据,不会改变表结构. 当数据库表存在时,如果不一样,则会修改表结构,原有的表结构不会改变.  create(很少):无论表结构是否存在,hibernate启动后都会重新生成表