如何换出hibernate.cfg.xml属性?

Ben*_*uer 7 orm hibernate properties configuration-files

我有一个Hibernate配置文件hibernate.cfg.xml,其中的硬编码属性名称如下:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">mysecretpassword</property>
    ...
  </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我想将用户名和密码等内容换成.properties-file.所以我会得到以下内容:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.username">${jdbc.username}</property>
    <property name="hibernate.connection.password">${jdbc.password}</property>
    ...
  </session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?对于Spring中dataSource,我可以我的中使用这个applicationContext.xml:

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />
Run Code Online (Sandbox Code Playgroud)

Hibernate的等价物是什么?

Pas*_*ent 3

如果这是一个选项,您可以从 中删除用户名和密码,并在放在类路径上的文件hibernate.cfg.xml中声明它们。hibernate.properties但您确实需要从 XML 配置文件中删除它们,因为它会覆盖“旧”属性文件中的属性。从文档中:

3.7. XML配置文件

另一种配置方法是在名为 的文件中指定完整配置 hibernate.cfg.xml。此文件可用作该 hibernate.properties文件的替换,或者如果两者都存在,则可覆盖属性。

如果这不是一个选项(并且如果您无法在 Spring 配置文件中配置 Hibernate),那么您必须在构建时使用构建工具(Ant、Maven 等)中的一些过滤功能来处理该问题。