Hibernate教程。连接被拒绝:连接

时间:2014-08-13 10:25:44

标签: java database-connection

我想了解Hibernate是如何工作的,所以找到this tutorial。但是我得到了这个错误:

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

我发现,很多人对同一个教程都有同样的问题。例如linkanother link

另一个问题是,启动程序(40 sek)后显示错误需要很长时间。

这是我的主要课程:

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配置文件:

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

员工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>  

和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;
}

}

这是我的罐子和所有文件。

Package Explorer

有人知道这里有什么问题吗?

2 个答案:

答案 0 :(得分:1)

在您的Hibernate配置文件中,映射已注释,

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

应该是更新,而不是create_drop

答案 1 :(得分:0)

我遇到了同样的错误。在我使用另一个hibernate maven tutorial的情况下,我只是忘了运行这样的服务:

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