Hibernate没有连接到数据库

时间:2013-07-22 14:40:25

标签: hibernate postgresql

所以我试图在localhost上连接我的postgresql数据库扔掉hibernate,但是当我尝试连接时总是会出错,真的不知道是什么问题。

> SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Initial SessionFactory creation failed.org.hibernate.HibernateException: /hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
    at persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
    at persistence.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.hp.videotheek.App.main(App.java:15)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2176)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2157)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
    at persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more

这是我从日食中获得的

    import org.hibernate.Session;

import persistence.HibernateUtil;

public class App 
{
    public static void main( String[] args )
    {
        Session session=HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
    }
}

这是我想要进行联系的地方

   package persistence;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new Configuration().configure().buildSessionFactory();
        }
        catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        getSessionFactory().close();
    }

}

enter image description here

这是我的文件设置

新错误

    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Initial SessionFactory creation failed.org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
    at persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
    at persistence.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at com.hp.videotheek.App.main(App.java:11)
Caused by: org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2246)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137)
    at persistence.HibernateUtil.buildSessionFactory(HibernateUtil.java:12)
    ... 2 more
Caused by: org.dom4j.DocumentException: Error on line 1 of document  : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2238)
    ... 5 more


    <!--<span class="hiddenSpellError" pre=""-->DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 <a href="http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"</a>>

 <hibernate-configuration>
 <session-factory>
 <!-- Database connection settings -->
 jdbc:postgresql://localhost:5432/postgres
 <property name="connection.driver_class">org.postgresql.Driver</property>
 <property name="connection.username">postgres</property>
 <property name="connection.password">****</property>
 <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

 <!-- SQL dialect - generate SQL for a particular database -->
 <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

 <!-- Echo all executed SQL statements -->
 <property name="show_sql">true</property>

 <!-- Enable Hibernate's automatic session context management -->
 <property name="current_session_context_class">thread</property>
<!-- Mapping resource configuration files -->
 <mapping resource="src/com/bookstore/bookapp.hbm.xml"/>
 </session-factory>
 </hibernate-configuration>

4 个答案:

答案 0 :(得分:0)

日志表明您需要hibernate.cfg.xml个文件。

答案 1 :(得分:0)

hibernate.cfg.xml需要(通常)在资源文件夹中。此资源文件夹由您的开发设置决定。我的资源位于src/main/resources文件夹中,我将IDE配置为将src/main/resources标记为源文件夹。

提供有关您的设置的更多详细信息将有助于我进一步回答您的问题。

答案 2 :(得分:0)

使用此代码创建会话工厂。

public class HibernateUltils {
static SessionFactory sessionFactory;       
  public static Session getSession()
  {
      try
      {
        SessionFactory sessionFactory = new  Configuration().configure().buildSessionFactory();
        session = sessionFactory.openSession();
        return session; 
      }
      catch(Exception e)
      {               
          e.printStackTrace();
      }
       return null;        
  }


  private static SessionFactory getSessionFactory(){
    return sessionFactory;
  }

在main方法中调用getSession()方法。

Session session = HibernateUtils.getSession();

答案 3 :(得分:0)

在您的新错误中,您没有xml标记:

 jdbc:postgresql://localhost:5432/postgres

应该是:

<property name="connection.url">jdbc:postgresql://localhost:5432/postgres</property>