【问题标题】:Hibernate Could not parse configuration: /hibernate.cfg.xmlHibernate 无法解析配置:/hibernate.cfg.xml
【发布时间】:2014-10-10 05:04:52
【问题描述】:

大家好,我是 hibernate 的初学者,我知道这里有很多类似的问题。我试图从他们那里解决,但我做不到。 我还尝试在 dtd 中将 SYSTEM 从 PUBLIC 更改,但它不起作用。 我用谷歌搜索它,但到处都显示 dtd 语句错误。

这是我的配置文件。

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
   <hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver/property> 
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 
<property name="username">root</property> 
<property name="password"></property>
<property name="connection.pool_size">1</property>  
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hbm2ddl.auto">update</property>
<mapping class="org.hibernate.src.userDetails" /> 
</session-factory>
</hibernate-configuration>

我尝试将版本 3.0 更改为 4.0,因为我使用的是休眠版本 4.3.6 但它仍然无法正常工作。 请帮帮我。

这是我的 userDetails 类..

包org.hibernate.src;

@Entity
public class userDetails {
@Id
private int userId ;
private String userName;
@Embedded
private Address address;


public Address getAddress() {
    return address;
}

public void setAddress(Address address) {
    this.address = address;
}

public int getUserId() {
    return userId;
}

public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public void setUserId(int userId) {
    this.userId = userId;

}


}

这些 jar 文件我已添加到我的项目中:

lib\jpa\hibernate-entitymanager-4.3.6.Final.jar
lib\required\antlr-2.7.7.jar
lib\required\dom4j-1.6.1.jar
lib\required\hibernate-commons-annotations-4.0.5.Final.jar
lib\required\hibernate-core-4.3.6.Final.jar
lib\required\hibernate-jpa-2.1-api-1.0.0.Final.jar
lib\required\jandex-1.1.0.Final.jar
lib\required\javassist-3.18.1-GA.jar
lib\required\jboss-logging-3.1.3.GA.jar
lib\required\jboss-logging-annotations-1.2.0.Beta1.jar
lib\required\jboss-transaction-api_1.2_spec-1.0.0.Final.jar

这是我在运行应用程序时得到的结果

Oct 13, 2014 4:24:47 PM      
 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
 INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
 Oct 13, 2014 4:24:47 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Environment buildBytecodeProvider  
INFO: HHH000021: Bytecode provider name : javassist
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
 Exception in thread "main" org.hibernate.HibernateException: Could not parse     
configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
at org.hibernate.test.userTest.main(userTest.java:18)
Caused by: org.dom4j.DocumentException: Error on line 4 of document  : Content is not    
allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
... 3 more

【问题讨论】:

    标签: hibernate nhibernate-mapping


    【解决方案1】:

    如你所说,我更新了我的 dtd。非常感谢。

    我更改了我的 标签。然后它显示标签前的空白错误。

    我解决了它然后它向我显示可访问数据库休眠的错误。我尝试了很多,即使我重新安装了我的 wamp 服务器,但它没有工作。

    最后我创建了一个新数据库并更改了我的 hibernate.cfg.xml 文件。

    这是我的文件,它工作得很好......

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
    <session-factory>
    <property name="show_sql">true</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost/test</property> 
    <property name="username">root</property> 
    <property name="password" /> 
    <property name="connection.pool_size">1</property>  
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hbm2ddl.auto">update</property>
    <mapping class="org.hibernet.src.userDetails" ></mapping> 
    </session-factory>
    </hibernate-configuration>
    

    【讨论】:

      【解决方案2】:

      需要互联网连接才能运行您的示例:

      <?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="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
              <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
              <property name="hibernate.connection.username">postgres</property>
              <property name="hibernate.connection.password">root</property>
              <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/db</property>
              <property name="connection_pool_size">1</property>
              <property name="hbm2ddl.auto">update</property>
              <property name="show_sql">true</property>
      
              <mapping resource="employee.hbm.xml" />
          </session-factory>
      
      </hibernate-configuration>
      

      不需要互联网连接即可运行您的示例:

      <?xml version='1.0' encoding='UTF-8'?>  
      
      <!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
      
      <hibernate-configuration>
      
          <session-factory>
      
              <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
              <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
              <property name="hibernate.connection.username">postgres</property>
              <property name="hibernate.connection.password">root</property>
              <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/springdb</property>
              <property name="connection_pool_size">1</property>
              <property name="hbm2ddl.auto">update</property>
              <property name="show_sql">true</property>
      
              <mapping resource="employee.hbm.xml" />
          </session-factory>
      
      </hibernate-configuration>
      

      这两个配置文件的唯一区别是具有不同的 DTD。

      【讨论】:

        【解决方案3】:

        问题出在下面一行。

        "-//Hibernate/Hibernate Configuration DTD .0//EN"
        

        应该是这样的:

        "-//Hibernate/Hibernate Configuration DTD 4.0//EN"
        

        Note:你在4.0中缺少4,你只有.0

        如果它不起作用,请将您的 DTD 更改为以下:

        <!DOCTYPE hibernate-mapping PUBLIC
                "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
        

        【讨论】:

        • 它显示以下错误...线程“main”中的异常 org.hibernate.MappingException:org.hibernate.cfg.Configuration.doConfigure 中的无效配置(Configuration.java:2158)在 org.hibernate .cfg.Configuration.configure(Configuration.java:2075) 在 org.hibernate.cfg.Configuration.configure(Configuration.java:2054) 在 org.firstHibernatepro.main.hibernateTest.main(hibernateTest.java:29)
        • 例外是关于类userDetails的映射,请确保您已正确完成类映射。此外,当您尝试从数据库保存或获取数据时,可能会引发此异常。
        【解决方案4】:

        DTD 声明应该是这样的:

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

        您必须使用3.0 进行DTD 声明,但不能使用4.0,因为根据this link from hibernate4.0 版本没有单独的DTD。

        Index of /dtd
        
        Name                                    Last modified               Size
        
        hibernate-mapping.dtd                   Fri Apr 4 13:56:57 2014     15K
        hibernate-configuration-2.0.dtd         Fri Apr 4 13:56:57 2014     1K
        hibernate-configuration-3.0.dtd         Fri Apr 4 13:56:57 2014     2K
        hibernate-mapping-3.0.dtd               Fri Apr 4 13:56:57 2014     44K
        hibernate-mapping-1.1.dtd               Fri Apr 4 13:56:57 2014     16K
        hibernate-configuration.dtd             Fri Apr 4 13:56:57 2014     830
        hibernate-mapping-2.0.dtd               Fri Apr 4 13:56:57 2014     25K
        hibernate-reverse-engineering-3.0.dtd   Fri Apr 4 13:56:57 2014     5K
        hibernate-generic.dtd                   Fri Apr 4 13:56:57 2014     3K
        

        您也可以参考此 SO 帖子以获取类似信息 - Hibernate error, possibly with DTD declaration

        更新:

        您的文件中的 DTD 声明不正确,您的文件为:

        <!DOCTYPE hibernate-mapping PUBLIC
            "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
            "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
        

        但应该是这样的:

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

        在你的文件中你说的是hibernate mapping,但它应该是hibernate configuration

        【讨论】:

        • thanx buddy...但是我已经尝试过这个东西但我仍然面临这个问题...我只是想知道需要为休眠应用程序添加哪些 jar 文件?
        • @Geek,您能否更新您的问题并添加异常的完整堆栈跟踪。还要将 hibernate Jars 的名称添加到您已包含在类路径中的问题中。
        • 嘿兄弟我已经添加了所有的细节..p
        • 嘿兄弟......我已经添加了所有细节......请让我知道我在哪里犯了错误??它会把我搞砸的......
        • @Geek,在我发表评论后,我没有看到您对问题所做的任何更改,那么您添加了什么?请添加这些 --> can you update your question and add the complete stacktrace of the exception. Also add the name of hibernate Jars to question that you have included in classpath 还添加您现在正在使用的最新 hibernate.cfg.xml 文件。如果不知道这些细节,就很难猜出问题出在哪里。
        【解决方案5】:

        试试这个。

        <!DOCTYPE hibernate-configuration SYSTEM 
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
        

        <!DOCTYPE hibernate-configuration SYSTEM 
        "http://www.hibernate.org/dtd/hibernate-configuration-4.0.dtd">
        

        Reference

        【讨论】:

          【解决方案6】:
          <!DOCTYPE root-node PUBLIC/SYSTEM "path to your DTD" >
          

          试试这个: 从您的 Hibernate jar 中提取 'hibernate-configuration-3.0.dtd' 并将其放在某个目录中(在这种情况下,我已添加到项目根目录中)。

          <!DOCTYPE hibernate-configuration SYSTEM
          "hibernate-configuration-3.0.dtd">
          

          它不会从网络上获取 DTD,而是从系统中获取 dtd。

          示例配置文件:

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE hibernate-configuration SYSTEM
          "hibernate-configuration-3.0.dtd">
          <hibernate-configuration>
          <session-factory>
          <property name="connection.driver_class">oracle.jdbc.xa.client.OracleXADataSource
          </property>
          <property name="connection.url">jdbc:oracle:thin:@localhost:9999:srvc</property>
          <property name="connection.username">username</property>
          <property name="connection.password">password</property>
          <property name="dialect">org.hibernate.dialect.OracleDialect</property>
          <property name="show_sql">true</property>
          <property name="hbm2ddl.auto">update</property>
          <mapping resource="\hibernate\products.hbm.xml"></mapping>
          </session-factory>
          </hibernate-configuration>
          

          【讨论】:

            【解决方案7】:

            您好,您之前的错误似乎是指向您的配置文件的第 4 行:

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

            应该是的

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

            结束标签中的&lt; 丢失

            【讨论】:

              【解决方案8】:

              可能是“http://www.hibernate.org”在您的计算机上被阻止或您的系统未连接到 Internet,因此解决方案是使用类路径在系统中提供 DTD 文件位置。 DTD 文件存在于休眠 jar 中,它应该从那里加载它。所以离线工作的 DocType 是:

              <!DOCTYPE hibernate-configuration SYSTEM 
                  "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
              

              【讨论】:

                【解决方案9】:

                在我的情况下,当我的数据库连接密码为空时,我遇到了同样的错误,如下所示:

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

                一旦我删除了属性这个"&lt;/property&gt;" 的右括号,如下行所示,它的工作原理:

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

                因此,如果您的数据库连接密码也为空,请尝试删除"&lt;/property&gt;" 括号。愿这也能解决你的问题。

                【讨论】:

                • 问题不是您的“”标签。但是如果你看到你已经通过在 标签中添加“/”来关闭你的标签(自我关闭标签)。所以你可以在这里使用任何一种方式..“”或“”。我希望它很清楚
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-04-06
                • 2018-05-13
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多