使用mysql进行Hibernate配置会产生ERROR - SAXParseException

时间:2011-05-27 10:11:43

标签: hibernate hibernate-mapping

我正在尝试使用mysql配置hibernate。

以下是hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>    
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/TestDB</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root123</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>


        <property name="hdm2ddl.auto">create</property>property>
        <property name="show_sql">false</property>


        <!-- Use the C3P0 connection pool provider -->
        <property name="hibernate.c3p0.min_size">5</property>
        <property name="hibernate.c3p0.max_size">20</property>
        <property name="hibernate.c3p0.timeout">300</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">3000</property>

        <mapping resource="com/psl/course/Course.hbm.xml"/>

    </session-factory>
</hibernate-configuration>

但是,我遇到了以下错误。堆栈跟踪如下,

15 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.1.GA
31 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
31 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
47 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
78 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
78 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
156 [main] ERROR org.hibernate.util.XMLHelper - Error parsing XML: /hibernate.cfg.xml(26) The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".
Initial SessionFactory creation failed.org.hibernate.MappingException: invalid configuration
Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.psl.util.HibernateUtil.(HibernateUtil.java:13)
    at com.psl.course.Main.saveCourse(Main.java:34)
    at com.psl.course.Main.main(Main.java:16)
Caused by: org.hibernate.MappingException: invalid configuration
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1521)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
    at com.psl.util.HibernateUtil.(HibernateUtil.java:10)
    ... 2 more
Caused by: org.xml.sax.SAXParseException: The content of element type "session-factory" must match "(property*,mapping*,(class-cache|collection-cache)*,event*,listener*)".
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanEndElement(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.dom4j.io.SAXReader.read(SAXReader.java:465)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1518)
    ... 5 more

请帮助,我哪里出错?

2 个答案:

答案 0 :(得分:5)

您在XML中输错:

<property name="hdm2ddl.auto">create</property>property>

答案 1 :(得分:1)

会话工厂的定义顺序必须遵循异常中的hte顺序

属性*,映射*,(类缓存|收集高速缓冲存储器)的,事件,听者*

我猜测发布的cfg.xml文件不是整个文件。 (原始文件中的第26行)