hibernate.cfg.xml 在项目中的位置? [英] Location of hibernate.cfg.xml in project?

查看:29
本文介绍了hibernate.cfg.xml 在项目中的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有以下结构的项目:

I created a project with following structure:

HibernateUtil:

HibernateUtil:

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration  configuration = new Configuration().configure( "C:\Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml");
            return new Configuration().configure().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();
    }

}

在线

Configuration  configuration = new Configuration().configure( "C:\Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml");

我有错误

初始SessionFactory创建failed.org.hibernate.HibernateException:C:UsersNikolay_TkachevworkspacehiberTestsrclogichibernate.cfg.xml在线程main"中未找到异常java.lang.ExceptionInInitializerError 在logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:19) 在logic.HibernateUtil.(HibernateUtil.java:9) 在logic.Main.main(Main.java:12) 引起的:org.hibernate.HibernateException:C:UsersNikolay_TkachevworkspacehiberTestsrclogichibernate.cfg.xml未在org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173)在org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1947)在 org.hibernate.cfg.Configuration.configure(Configuration.java:1928)在 logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:14)... 2个

Initial SessionFactory creation failed.org.hibernate.HibernateException: C:UsersNikolay_TkachevworkspacehiberTestsrclogichibernate.cfg.xml not found Exception in thread "main" java.lang.ExceptionInInitializerError at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:19) at logic.HibernateUtil.(HibernateUtil.java:9) at logic.Main.main(Main.java:12) Caused by: org.hibernate.HibernateException: C:UsersNikolay_TkachevworkspacehiberTestsrclogichibernate.cfg.xml not found at org.hibernate.internal.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:173) at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1947) at org.hibernate.cfg.Configuration.configure(Configuration.java:1928) at logic.HibernateUtil.buildSessionFactory(HibernateUtil.java:14) ... 2 more

错误的原因是什么,我该如何解决?

What is the reason for the error and how do I fix it?

推荐答案

给出相对于你的项目的路径.

Give the path relative to your project.

在您的 src 中创建一个名为 resources 的文件夹并将您的配置文件放在那里.

Create a folder called resources in your src and put your config file there.

   configuration.configure("/resources/hibernate.cfg.xml");

如果你检查你的代码

Configuration  configuration = new Configuration().configure( "C:\Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml");
return new Configuration().configure().buildSessionFactory();

您在两行中创建了两个配置对象.

In two lines you are creating two configuration objects.

如果你写,那应该可以工作(没有测试过),

That should work(haven't tested) if you write,

Configuration  configuration = new Configuration().configure( "C:\Users\Nikolay_Tkachev\workspace\hiberTest\src\logic\hibernate.cfg.xml");
return  configuration.buildSessionFactory();

但是在服务器上部署后失败,因为您使用的是系统路径而不是项目相对路径.

But It fails after you deploy on the server,Since you are using system path than project relative path.

这篇关于hibernate.cfg.xml 在项目中的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆