java - hibernate 教程。连接被拒绝 : connect

标签 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

我发现,很多人在使用相同的教程时都遇到相同的问题。例如link ,或another link .

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

这是我的主要类(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 配置文件:

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

}

这是我的 jar 和所有文件。

Package Explorer

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

最佳答案

在 Hibernate 配置文件中,映射被注释,并且

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

应该是更新,而不是create_drop

关于java - hibernate 教程。连接被拒绝 : connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25283766/

相关文章:

java - 在 Apache Ant 中使用 <java> 标签,我可以同时运行 Server 和 Proxy Server 类吗

php无法连接到mysql

mysql - Visual Studio 2013 连接到本地 MySQL 数据库

java - 使用spring boot动态连接到不同的数据库

java - 如何在方法调用期间使用同一类的两个对象?

java - Apache Spark 需要 5 到 6 分钟才能从 Cassandra 简单地计算 10 亿行

.net - Advantage 数据库服务器 INSERT/UPDATE 查询性能问题

django - 如何调试 "could not receive data from client: Connection reset by peer"

java - 如何搜索分成两行的单词?

java - 安卓开发: Directing to specific tabs in fragment