【问题标题】:hibernate.cfg.xml not found (eclipse)未找到 hibernate.cfg.xml (eclipse)
【发布时间】:2015-02-07 10:20:43
【问题描述】:

我今天开始使用 Hibernate 并测试了一个简单的示例,但出现错误:找不到 hibernate.cfg.xml。 我将 hibernate.cfg.xml 文件放在 src 文件夹中,这是它的内容:

    <?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >

<hibernate-configuration>

  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/test</property>
    <property name="connection.username">test</property>
    <property name="connection.password">test</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

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

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

    <mapping resource="com/mycompany/model/Product.hbm.xml"/>

  </session-factory>
</hibernate-configuration>

我将 HibernateUtil.java 文件放在 util 文件夹 (src/util) 下,这是它的内容

    package util;

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

public class HibernateUtil
{
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory()
    {
        try
        {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure("hibernate.cgf.xml").buildSessionFactory();
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }
}

我还将 Jar 添加到构建路径中。

我的类Test.java:

    import ...


public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ProductDao pd = new ProductDao();

        try {

            Product p = new Product("PC", 1000L);

            pd.add(p);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

添加方法:

公共无效添加(产品 p){

Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
session.beginTransaction();
session.save(p);
tx.commit();

}

提前致谢

【问题讨论】:

  • hibernate.cfg.xml 放入src-&gt;util 文件夹,然后重试。
  • 你使用的是哪个构建工具,比如 Gradle、Ant、Maven
  • 我做到了,但还是同样的问题。
  • 我没有使用任何工具。

标签: java eclipse hibernate


【解决方案1】:

如果它是一个 Maven 项目,只需在 eclips 的 /src/main/resources 中添加 cfg.xml 文件

【讨论】:

    【解决方案2】:

    如果使用maven,请尝试将其放入

    /src/main/resources
    

    如果使用简单的eclipse项目(没有maven),把它放在项目根目录下(不在src里)

    另外你在源文件中拼错了文件名

    return new Configuration().configure("hibernate.cgf.xml").buildSessionFactory();
    

    一定是

    hibernate.cfg.xml
    

    【讨论】:

    • 我就是这样做的,在src文件夹中
    • 不,我没有使用 maven
    • 见上图
    • 还是同样的问题...!
    • 我应该将它添加到构建路径还是类似的东西?
    猜你喜欢
    • 2012-01-02
    • 2011-06-23
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2012-03-16
    相关资源
    最近更新 更多