用Hibernate(Eclipse)连接到postgreSQL

时间:2017-04-04 19:24:42

标签: java eclipse postgresql hibernate

我做错了什么?运行主类后,我在控制台中查看,如下面的当前屏幕截图,控制台继续工作,没有任何反应。

hibernate.cfg.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-configuration>
  <session-factory>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/Gillie_PL</property>
    <property name="connection.username">postgres</property>
    <property name="connection.password">postgres</property>
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">update</property>
   </session-factory>
</hibernate-configuration>

hibernateUtil.class:

package hibernateConn;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
   private static SessionFactory sessionFactory = null;
   static {
     Configuration cfg =
      new Configuration().configure("/hibernateConn/hibernate.cfg.xml");
      StandardServiceRegistryBuilder builder =
      new StandardServiceRegistryBuilder()
      .applySettings(cfg.getProperties());
      sessionFactory = cfg.buildSessionFactory(builder.build());
   }

 public static SessionFactory getSessionFactory(){
   return sessionFactory;
 }

}

Main.Class:

package hibernateConn;
import org.hibernate.SessionFactory;
public class Main {
  public static void main(String[] args) {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
   }
}

运行Main.class后,我在控制台中:

enter image description here

就是这样。没有更多,程序继续工作,没有任何反应,好像递归发生...也许我没有正确指定xml中的设置?

1 个答案:

答案 0 :(得分:0)

我重写了HibernateUtil.class:

static {
    try{
        sessionFactory = new Configuration().configure("/hibernateConn/hibernate.cfg.xml").buildSessionFactory();
    }catch(Throwable ex){
        System.err.println("++++Initial SessionFactory creation failed.++++: " + ex);
        ex.printStackTrace();
    }
}

public static SessionFactory getSessionFactory(){
    return sessionFactory;
}

这一切都奏效了。问题可以结束。