hibernate.cfg.xml没有找到hibernate.properties文件 [英] hibernate.cfg.xml does not find hibernate.properties file

查看:130
本文介绍了hibernate.cfg.xml没有找到hibernate.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们曾经在我们的Java源文件夹的同一个文件夹中有hibernate.cfg.xml和hibernate.properties文件。现在我将hibernate.properties文件移动到另一个位置。我如何使hibernate.cfg.xml文件再次找到hibernate.properties文件?我收到一个错误,似乎表明它不再被发现。

解决方案

编程方式,您可以像这样加载XML和属性:

  public class MyHibernate {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory(){
try {
URL r1 = MyHibernate.class.getResource(/ hibernate.cfg.xml);
配置c =新配置()。configure(r1);

try {
InputStream is = MyHibernate.class.getResourceAsStream(/ hibernate.properties);
属性props = new Properties();
props.load(is);
c.addProperties(道具);
} catch(Exception e){
LOG.error(Error reading properties,e);
}

return c.buildSessionFactory();
} catch(Throwable ex){
LOG.error(Error Creating SessionFactory,ex);
抛出新的ExceptionInInitializerError(ex);



public static SessionFactory getSessionFactory(){
return sessionFactory;


希望它有用。


We used to have the hibernate.cfg.xml and hibernate.properties files in the same folder in our Java source folder. Now I moved the hibernate.properties file to another location. How can I make the hibernate.cfg.xml file find the hibernate.properties file again? I get an error which seems to indicate that it is not found anymore.

解决方案

Programmatically, you can load XML and properties like this:

public class MyHibernate {

  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    try {
        URL r1 = MyHibernate.class.getResource("/hibernate.cfg.xml");
        Configuration c = new Configuration().configure(r1);

        try {
            InputStream is = MyHibernate.class.getResourceAsStream("/hibernate.properties");
            Properties props = new Properties();
            props.load(is);
            c.addProperties(props);
        } catch (Exception e) {
            LOG.error("Error reading properties", e);
        }

        return c.buildSessionFactory();
    } catch (Throwable ex) {
        LOG.error("Error creating SessionFactory", ex);
        throw new ExceptionInInitializerError(ex);
    }
  }

  public static SessionFactory getSessionFactory() {
    return sessionFactory;
  }
}

Hope it be useful.

这篇关于hibernate.cfg.xml没有找到hibernate.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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