views:

30

answers:

1

Hello all!

I've got a hibernate configuration file called hibernate.cfg.xml. In this file there are hard-coded property names like:

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

I want to swap out things like the username and the password to a ".properties"-file. So that I will get the following:

<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.username">${jdbc.username}</property>
    <property name="hibernate.connection.password">${jdbc.password}</property>
    ...
  </session-factory>
</hibernate-configuration>

How can I do that? For the dataSource in Spring I can use this one in my applicationContext.xml:

<bean id="propertyConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
      p:location="/WEB-INF/jdbc.properties" />

What is the equivalent for Hibernate? Does anybody know this?

Best wishes

Benny

+1  A: 

If this is an option, you could remove the username and password from the hibernate.cfg.xml and declare them in an hibernate.properties file that you put on the classpath. But you really need to remove them from the XML configuration file as it overrides properties from the "legacy" properties file. From the documentation:

3.7. XML configuration file

An alternative approach to configuration is to specify a full configuration in a file named hibernate.cfg.xml. This file can be used as a replacement for the hibernate.properties file or, if both are present, to override properties.

If this is not an option (and if you can't configure Hibernate in your Spring configuration file), then you'll have to handle that at build time, using some filtering features from your build tool (Ant, Maven, etc).

Pascal Thivent
Ok. I put the "hibernate.properties" file in my "/src/java" directory and now it works. Thank you :) But I want to have my jdbc.properties and my hibernate.properties together in one file (because they both have the same setting for username and password). Is there a possibility to do that? Because with the "location" property of my "PropertyPlaceholderConfigurer" I cannot access "/src/java". I'm limited to my "/web" folder which is on a different level.
Benny Neugebauer
@Benny Some questions: 1. why does hibernate not use the datasource? 2. why don't you configure hibernate using Spring?
Pascal Thivent
Hi Pascal. This is a very good question. I do such things because I'm very new to Hibernate and Spring. Therefore I must try out some things first. Having two seperate configuration files was the fastest solution which I have found. If I want to use the datasource for Hibernate then I have to reference it am I right? In some hibernate configs I have seen settings like <property name="connection.datasource">NameOfDataSourceBean</property>. I will check, whether this works...
Benny Neugebauer
@Benny Neugebauer If the answer fullfil your needs, mark as accepted. Otherwise your accpet rate will become 0% and other users will not answer your questions anymore. And the acceptance rate by tag will be reduced (+1) Pascal and i hope Benny Neugebauer plays your role as Stackoverflow user
Arthur Ronald F D Garcia