0
votes

i am new to hibernate and tried to execute a simple hibernate jave code but unfortunately i am getting this exception. Somebody has written may be its due to the DOCTYPE , i have used in my configuration file.This is my hibernate.cfg.xml file:

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

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

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

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

        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>
        <mapping resource="employee.hbm.xml" />
    </session-factory>

</hibernate-configuration> 

and the exception is :

Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491) at org.hibernate.cfg.Configuration.configure(Configuration.java:1425) at com.javatpoint.mypackage.StoreData.main(StoreData.java:13) Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) ... 2 more

2
please add the full stacktrace.Jens
Post your Doctype declaration ,in hibernate.cfg.xmlNeeraj Jain
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "hibernate.org/dtd/hibernate-configuration-3.0.dtd">Rahul Vashishta
I hope your are not missing as root tag <hibernate-configuration>Abhiram mishra
no,it is present there.Rahul Vashishta

2 Answers

0
votes
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

Your DOCTYPE is using the new namespace (http://www.hibernate.org/dtd/) for Hibernate 3.6 , and you might have an older version of the Hibernate libraries in your classpath.

Replace it with

<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
0
votes

This one is a temporary solution. Your Hibernate jars contains dtd for validating your configuration xml. Extract ‘hibernate-configuration-3.0.dtd’ and put it in some directory in your project structure (in this case, I have put it in Project root directory). Add your dtd location to DOCTYPE declaration.

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

It worked for me. It works when system is offline. Fetches DTD from your local system.

Its just that we have to figure a way to fetch dtd from your jar.

you may do it this way :

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

but then, it is throwing

Caused by: org.dom4j.DocumentException: unknown protocol: classpath Nested exception: unknown protocol: classpath