Hibernate无法解析配置:/hibernate.cfg.xml

时间:2014-10-10 05:04:53

标签: hibernate nhibernate-mapping

嘿伙计们我是hibernate的初学者,我知道这里有很多类似的问题。我试图从他们那里解决,但我不能。 我也尝试在dtd中从PUBLIC更改SYSTEM但它不起作用。 我用谷歌搜索了它,但它显示了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,因为我使用的是hibernate版本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

9 个答案:

答案 0 :(得分:3)

我按照你提到的更新了我的dtd。非常感谢你。

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

我解决了它然后它向我显示了可访问数据库hibernate的错误。我尝试了很多甚至我重新安装我的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>

答案 1 :(得分: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。

答案 2 :(得分:1)

问题出在以下几行。

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

应该如下:

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

Note:您在4中遗失了4.0,您只有.0

如果无效,请将DTD更改为以下内容:

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

答案 3 :(得分:1)

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 hibernate,没有单独的DTD与4.0版本。

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

答案 4 :(得分:0)

尝试使用它。

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

OR

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

Reference

答案 5 :(得分:0)

<!DOCTYPE root-node PUBLIC/SYSTEM "path to your DTD" >

试试这个: 从Hibernate jar中提取&#39; hibernate-configuration-3.0.dtd&#39; 并将其放在某个目录中(在本例中,我已添加到Project根目录)。

<!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>

答案 6 :(得分:0)

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

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

应该是

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
结束标记中的

<缺失

答案 7 :(得分:0)

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

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

答案 8 :(得分:0)

在我的情况下,当我使用关闭标记的数据库连接的空密码时,我遇到了同样的错误:

"</property>"

一旦我删除了属性的右括号,就像下面一行<property name="hibernate.connection.password"/> 一样,它正在工作:

"</property>"

因此,如果您还有用于数据库连接的空密码,请尝试删除Private Sub Worksheet_Change(ByVal Target As Range) Dim rIn As Range, r As Long Set rIn = Range("B2:C9999") If Intersect(rIn, Target) Is Nothing Then Exit Sub If Target.Count > 1 Then Exit Sub r = Target.Row If Cells(r, 1) <> "" Then Exit Sub If Cells(r, 2) = "" Or Cells(r, 3) = "" Then Exit Sub Application.EnableEvents = False Cells(r, 1) = Date Application.EnableEvents = True End Sub 括号。愿这也能解决你的问题。