【问题标题】:Hibernate not connecting to database休眠未连接到数据库
【发布时间】:2013-07-22 14:40:25
【问题描述】:

所以我试图连接到我在 localhost 上的 postgresql 数据库引发了休眠,但是当我尝试连接时总是出错并且真的不知道问题是什么。

> 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

这是我在 eclipse 中登录得到的结果

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

}

这是我的文件设置

新错误

    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>

【问题讨论】:

  • 详情请添加hibernate.cfg.xml。
  • 添加了配置文件
  • &lt;!--&lt;span class="hiddenSpellError" pre=""--&gt;DOCTYPE

标签: hibernate postgresql


【解决方案1】:

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

 jdbc:postgresql://localhost:5432/postgres

应该是:

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

【讨论】:

    【解决方案2】:

    日志说你需要hibernate.cfg.xml文件。

    【讨论】:

    • 但是文件放在哪里我用主src/main/java放了
    【解决方案3】:

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

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

    【讨论】:

    • 我会将您的资源移动到 src/main/resources 并配置 eclipse 以将 src/main/resources 标记为 src 文件夹。
    • 另外,看看构建输出,hibernate.cfg.xml 存在哪里?
    • 我将资源文件夹添加到我的构建路径中,但现在出现了一个新错误
    【解决方案4】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2023-03-29
      • 2015-10-27
      • 2012-08-04
      • 1970-01-01
      相关资源
      最近更新 更多