Hibernate Could not parse configuration: /hibernat

2019-04-03 01:53发布

问题:

Hey guys I am beginner for the hibernate and I know there are so many similar questions here. I tried to solve from them but I could not. I also tried to change SYSTEM from PUBLIC in dtd but its not working. I googled for it but everywhere it showing for the dtd statement error.

This is my configuration file.

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

I tried to change the version 3.0 to 4.0 as I am using hibernate version 4.3.6 but still its not working. please help me out.

This is my userDetails class..

package 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;

}


}

These jar files I have added to my project:

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

This is what I am getting while running the application

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

回答1:

I updated my dtd as u mentioned. thank you so much.

I changed my tag. then it was showing an error for white space bfore the tag.

i solved it then it was showing me an error for accessible database hibernate. i tried a lot even i reinstall my wamp server, though it didnt work.

finally i create a new database and changed my hibernate.cfg.xml file.

this is my file which is working cool...

<?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:

Require internet connection to run your example:

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

Don't require internet connection to run your example:

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

Only difference between these two config file is having different DTD.



回答3:

The problem is in following line.

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

It should be as following:

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

Note: You are missing 4 in 4.0, you just have .0

If it does not work change your DTD to following:

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


回答4:

The DTD declaration should be like this:

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

you have to use 3.0 for DTD declaration but not 4.0 because as per this link from hibernate there is no separate DTD with 4.0 version.

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

Also you can refer to this SO post for similar information - Hibernate error, possibly with DTD declaration

Update:

The DTD declaration is not correct in your file, you have it as:

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

but it should be like this:

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

In your file you are saying hibernate mapping but it should be hibernate configuration



回答5:

Try using this.

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



回答6:

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

Try this one : Extract 'hibernate-configuration-3.0.dtd' from your Hibernate jar and put it in some directory (in this case, I have added to Project root directory).

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

Instead of fetching DTD from the web, it will fetch the dtd from the system.

Sample Configuration file :

<?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:

Hi it seems your previous error was pointing you to line 4 of your configuration file:

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

it should have been

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

< in the closing tag was missing



回答8:

It may be "http://www.hibernate.org" is blocked on your machine or your system is not connected to internet so the solution is to provide the DTD file location in the system using classpath. DTD file is present in the hibernate jars and it should load it from there. So the DocType that worked offline would be:

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


回答9:

In my case I had the same error when I had empty password for database connection with closing tag like below:

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

Once I removed the closing brackets of property this one "</property>" like in below line, its working:

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

So if you also have empty password for database connection try to remove "</property>" brackets. May this will solve your problem too.