简体   繁体   中英

Cant connect to postgreSQL with Hibernate(Eclipse)

What i do wrong? After running main class i have view in console like in current screenshot below and the console continues to work and nothing happens.

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();
   }
}

After I run Main.class I have in console:

在此处输入图片说明

And that's all. Nothing more, the program continues to work and nothing happens, as if recursion occurs ... Maybe I did not correctly specify the settings in xml?

I rewrote the 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;
}

and it all worked. The question can be closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM