如何修复Hibernate的配置文件?

时间:2013-04-17 08:22:18

标签: java xml hibernate

执行我的java代码后,我收到以下错误。似乎只有XML文件存在问题。所以,我没有发布Java代码。错误后给出了XML代码。我按原样从我的书中复制了它但却导致错误。我如何纠正这个烦人的xml错误?

"Some value of time" org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
[main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
[main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
[main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
[main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
[main] INFO org.hibernate.cfg.Configuration - configuring from resource:     
/hibernate.cfg.xml
[main] INFO org.hibernate.cfg.Configuration - Configuration resource:     
 /hibernate.cfg.xml
[main] ERROR org.hibernate.util.XMLHelper - Error parsing XML:     
/hibernate.cfg.xml(3)     
The markup in the document preceding the root element must be well-formed.
Exception in thread "main" org.hibernate.HibernateException: Could not parse     
configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure
    (AnnotationConfiguration.java:990)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure
    (AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.AnnotationConfiguration.configure
    (AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure
    (AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure
    (AnnotationConfiguration.java:966)
at com.examscam.model.User.persist(User.java:47)
at com.examscam.model.User.main(User.java:57)
    Caused by: org.dom4j.DocumentException: Error on line 3 of document  : The markup  
    in the document preceding the root element must be well-formed. Nested exception:
    The markup in the document preceding the root element must be well-formed.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1532)
... 9 more

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-3.0.dtd">

<hibernate-configuration>
<session-factory>

<property name = "connection.url">
jdbc:mysql://localhost/examscam
</property>

<property name = "connection.username">
root
</property>

<property name = "connection.password">
password
</property>

<property name = "connection.driver_class">
com.mysql.jdbc.Driver
</property>

<property name = "dialect">
org.hibernate.dialect.MySQLDialect
</property>

<property name = "transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>

<property name = "current_session_context_class">
thread
</property>

<property name = "hibernate.show_sql">
true
</property>

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

修改

org.hibernate.cfg.annotations.Version <clinit>
INFO: Hibernate Annotations 3.3.1.GA
[main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.2.GA
[main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
[main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
[main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
[main] INFO org.hibernate.cfg.Configuration - configuring from resource:
 /hibernate.cfg.xml
[main] INFO org.hibernate.cfg.Configuration - Configuration resource: 
/hibernate.cfg.xml
[main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : 
com/xx  /portal/db/User.hbm.xml
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: com/xx
/portal/db/User.hbm.xml not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:610)
at org.hibernate.cfg.AnnotationConfiguration.addResource
(AnnotationConfiguration.java:912)
at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement
(AnnotationConfiguration.java:617)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1603)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1582)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure
(AnnotationConfiguration.java:1002)
at    org.hibernate.cfg.AnnotationConfiguration.doConfigure
(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1556)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure
(AnnotationConfiguration.java:990)
at org.hibernate.cfg.AnnotationConfiguration.doConfigure
(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1476)
at org.hibernate.cfg.AnnotationConfiguration.configure
(AnnotationConfiguration.java:972)
at org.hibernate.cfg.AnnotationConfiguration.configure
(AnnotationConfiguration.java:69)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure
(AnnotationConfiguration.java:966)
at com.examscam.model.User.persist(User.java:45)
at com.examscam.model.User.main(User.java:55) 

1 个答案:

答案 0 :(得分:0)

这是我的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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://127.0.0.1:3306/test
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">123456</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <!-- disable cache  -->
        <property name="hibernate.cache.use_second_level_cache">
            false
        </property>

        <!-- UTF8 to database -->
        <property name="connection.useUnicode">true</property>
        <property name="connection.characterEncoding">UTF-8</property>

        <!-- C3P0 Session Manager  -->
        <property name="hibernate.connection.provider_class">
            org.hibernate.connection.C3P0ConnectionProvider
        </property>
        <property name="hibernate.c3p0.min_size">1</property>
        <property name="hibernate.c3p0.max_size">2</property>
        <property name="hibernate.c3p0.timeout">1800</property>
        <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.c3p0.idle_test_period">300</property>

        <!-- Entity Mappings -->
        <mapping resource="com/xx/portal/db/User.hbm.xml" />

    </session-factory>

</hibernate-configuration>