PRO_gramista
PRO_gramista

Reputation: 922

Hibernate tutorial. Connection refused: connect

I want to learn how Hibernate works, so found this tutorial. But i get this error:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
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 mypackage.StoreData.main(StoreData.java:14)
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

I found out, that many people have the same issue with the same tutorial. For example link, or another link.

Another issue is, that it takes far to long to show the error after starting the programm(40 sek).

Here is my main class:

package mypackage;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class StoreData {
public static void main(String[] args) {

    try {
        // creating configuration object
        Configuration cfg = new Configuration();
        cfg.configure("hibernate.cfg.xml");// populates the data of the

        // configuration file

        // creating seession factory object
        SessionFactory factory = cfg.buildSessionFactory();

        // creating session object
        Session session = factory.openSession();

        // creating transaction object
        Transaction transaction = session.beginTransaction();

        Employee e1 = new Employee();
        e1.setId(115);
        e1.setFirstName("Elvis");
        e1.setLastName("Presley");

        session.persist(e1);// persisting the object

        transaction.commit();// transaction is committed
        session.close();

        System.out.println("successfully saved");
    }catch (HibernateException ex) {
        ex.printStackTrace();
    } 
    catch (Exception ex) {
        ex.printStackTrace();
    }
}
}  

Hibernate configuration 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">
<hibernate-configuration>
<session-factory>
    <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url">jdbc:sqlserver://127.0.0.1:1433;databaseName=hibernatetutorial;</property>
    <property name="hibernate.connection.username">username</property>
    <property name="hibernate.connection.password">password</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="show_sql">true</property>
    <mapping resource="employee.hbm.xml" />
</session-factory>

</hibernate-configuration>  

Employee xml:

 <?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>  
  <class name="mypackage.Employee" table="emp1000">  
    <id name="id">  
     <generator class="assigned"></generator>  
    </id>  

    <property name="firstName"></property>  
    <property name="lastName"></property>  

  </class>  

 </hibernate-mapping>  

and Employee.java:

package mypackage;

public class Employee {

private int id;
private String firstName; 
private String lastName;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

}

And this are my jars and all files.

Package Explorer

Someone knows what is the issue here?

Upvotes: 0

Views: 1492

Answers (2)

Bechyň&#225;k Petr
Bechyň&#225;k Petr

Reputation: 795

I was facing the same error. In my case with another hibernate maven tutorial, I just forgot to run the service like this:

mvn exec:java -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0 file:target/data/tutorial"

Upvotes: 0

user2786496
user2786496

Reputation: 67

in your Hibernate configuration file, mapping is commented, and

 <property name="hibernate.hbm2ddl.auto">create_drop</property>

is supposed to be update, not create_drop

Upvotes: 1

Related Questions