【问题标题】:How to specify another properties file other than hibernate.properties如何指定除 hibernate.properties 之外的另一个属性文件
【发布时间】:2012-01-19 03:58:32
【问题描述】:

我有一个产品的“hibernate.properties”,我还想要一个用于测试。

所以我在类路径中创建了一个“hibernate.test.properties”,但我不知道如何让hibernate使用它。它总是使用“hibernate.properties”。

我能做什么?

【问题讨论】:

    标签: hibernate


    【解决方案1】:

    学习这个 HibernateUtil 类。它加载没有标准名称的 XML 和属性文件。希望有用。

    public class HibernateUtil2 {
      private static final SessionFactory sessionFactory = buildSessionFactory();
    
      private static SessionFactory buildSessionFactory() {
        try {
            URL r1 = HibernateUtil2.class.getResource("/hibernate2.cfg.xml");
            Configuration c = new Configuration().configure(r1);
    
            try {
                InputStream is = HibernateUtil2.class.getResourceAsStream("/hibernate2.properties");
                Properties props = new Properties();
                props.load(is);
                c.addProperties(props);
            } catch (Exception e) {
                LOG.error("Error al leer las propiedades", e);
            }
    
            return c.buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            LOG.error("Error al construir SessionFactory", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
    
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 2012-03-24
      • 2016-02-19
      相关资源
      最近更新 更多