0

I am trying to connect to mysql database using hibernate from the cfg file as

   <?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.connection.driver_class">
            com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">
            jdbc:mysql://localhost:3306/test</property>
        <property name="hibernate.connection.username">
            root</property>
        <property name="hibernate.connection.password">
            root</property>
        <property name="hibernate.connection.pool_size">
            10</property>
        <property name="show_sql">true</property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">
            update</property>

        <!-- Mapping files -->

        <mapping resource="Employee.hbm.xml" />
    </session-factory>
</hibernate-configuration>





   private static SessionFactory factory;

        public static void main(String[] args) {
            try {
                factory = new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                System.err.println("Failed to create sessionFactory object." + ex);
                ex.fillInStackTrace();
                ex.printStackTrace();
                throw new ExceptionInInitializerError(ex);
            }

Though my connection-string,username,password are correct and the same are working fine in other project with basic jdbc connection, But when I try it using hibernate I am getting this error on console

 Exception in thread "main" java.lang.ExceptionInInitializerError
    at com.hibernate.ManageEmployee.main(ManageEmployee.java:21)
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at com.hibernate.ManageEmployee.main(ManageEmployee.java:19)
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)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1425)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1411)
    at com.hibernate.ManageEmployee.main(ManageEmployee.java:16)

And yes the database server is up and running but the connections from this hibernate application are refused

Rishabh
  • 3,752
  • 4
  • 47
  • 74
  • Are you created the password for test db. If not create password for test database – UdayKiran Pulipati Mar 25 '14 at 12:30
  • 1
    There is no mention about hibernate is trying to connect in your stack trace (just configuring).My guess is that your XML parser is trying to connect somewhere to get DTD/XSD for validation.Can you provide full stacktrace? Also check this one http://stackoverflow.com/questions/17114046/hibernate-error-possibly-with-dtd-declaration – rkosegi Mar 25 '14 at 12:32
  • @udaykiranpulipati : I have created the password already – Rishabh Mar 25 '14 at 12:35
  • @rkosegi : check the edit I have added the full stacktrace – Rishabh Mar 25 '14 at 12:47
  • @Rishabh where you place hibernate.cfg.xml. If it is not in src root folder, Place it there. – UdayKiran Pulipati Mar 25 '14 at 12:56
  • @udaykiranpulipati : it is in the src root folder – Rishabh Mar 25 '14 at 12:57
  • Try to put it in "WEB-INF/classes" directory – mareckmareck Mar 25 '14 at 13:15
  • @mareckmareck the configuration file is found that's not the problem. The problem seems to be the DTDResolver. Maybe Doctype and used library don't match. Old Libraries used the namespace `http://hibernate.sourceforge.net/`newer ones (3.6) use `http://www.hibernate.org/dtd/` – andih Mar 25 '14 at 13:23
  • verify Your active provider like here [eclipse-prefrences-network-connections](http://stackoverflow.com/questions/5456480/eclipse-prefrences-network-connections) – cane Oct 19 '15 at 08:44

1 Answers1

1

The problem is that the xml parser is trying to access the schema file which is specified in the configuration file. Can the box, where you are running your program, access the internet?

Here is some additional information:

https://forum.hibernate.org/viewtopic.php?f=1&t=949031

Steven Pessall
  • 983
  • 5
  • 15
  • 3
    Hibernate can resolve the DTDs locally (without a network connection). See for example here http://stackoverflow.com/questions/4301294/cant-parse-hibernate-cfg-xml-while-offline – andih Mar 25 '14 at 13:19