java - 使用 hibernate.cfg.xml 时更改 hibernate.properties 文件的位置

标签 java spring hibernate

我们在 hibernate.cfg.xml 中提供数据库凭证

<hibernate-configuration>
    <session-factory>
       <property name="hibernate.connection.url">url</property>
       <property name="hibernate.connection.username">username</property>
       <property name="hibernate.connection.password">password</property>
    <session-factory>
<hibernate-configuration>

我们可以在此处或在类路径中的 hibernate.properties 中提供这些属性。但我希望它们来自外部文件。我在 hibernate 中找不到更改默认 hibernate.properties 文件路径的方法。

请帮忙。

[编辑] java中生成sessionFactory对象的方法

public class HibernateUtil {

  private static final SessionFactory sessionFactory = buildSessionFactory();

  private static SessionFactory buildSessionFactory() {
    // Create the session factory from hibernate.cfg.xml
    Configuration configuration = new Configuration();
    StandardServiceRegistryBuilder serviceRegistryBuilder =
        new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    return configuration.buildSessionFactory(serviceRegistryBuilder.build());
  }

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

最佳答案

以编程方式,您可以加载 XML 和属性,如下所示:

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

整个 Spring

您可以使用 PropertiesFactoryBean 从文件中读入属性并配置 LocalSessionFactoryBean:

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
    <property name="hibernateProperties">
      <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="location">path-to-properties-file</property>
      </bean>
    </property>
    ...
  </bean>

希望它有用。

关于java - 使用 hibernate.cfg.xml 时更改 hibernate.properties 文件的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25041715/

相关文章:

java - 如何在 Java 的 JPA 查询中使用 IN 子句?

java - 为什么我无法访问这个放入 Model 对象的列表到这个 Spring MVC 项目中?

java - Hibernate搜索依赖异常java.lang.AbstractMethodError : null

java - 没有数据时应该返回哪个http代码

java - 允许一次登录一台设备

java - 应用上下文。这是什么?

java - UTF-8 与 JPA 和 Glassfish 4.0

java - Hibernate/JPA - 对象本身的外键索引

java - 我们如何在不借助父类的帮助的情况下将变量从同一个类中的一个方法传递到另一个方法?

java - 图像超出 ImageButton