Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include properties from external file to hibernate.cfg.xml?

I need to be able to store database config properties in src|main|java|dbConnection.properties and include it to hibernate.cfg.xml in form of jstl expressions. (like : ${password} etc.). How to do it?

Current hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">postgres</property>
        <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    </session-factory>
</hibernate-configuration>

I need something like this:

<?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD//EN"
            "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
        <session-factory>
    <property name="connection.driver_class">${DRIVER}</property>
            <property name="connection.username">${USERNAME}</property>
            <property name="connection.password">${PASSWORD}</property>
            <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        </session-factory>
    </hibernate-configuration>
like image 838
J.Olufsen Avatar asked Jun 12 '14 03:06

J.Olufsen


People also ask

Can we have Hibernate properties instead of Hibernate cfg XML?

cfg. xml . This file can be used as a replacement for the hibernate. properties file or, if both are present, to override properties.

Where should I put Hibernate cfg XML?

Let us create hibernate. cfg. xml configuration file and place it in the root of your application's classpath.


1 Answers

You state that you use Spring then why not let Spring do all the hard work. Let a property placeholder replace the placeholders you want.

<context:property-placeholder location="classpath:dbConnection.properties" />

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="hibernateProperties">
       <map>
            <entry key="connection.driver_class" value="${DRIVER}" />
            <entry key="connection.username" value="${USERNAME}" />
            <entry key="connection.password" value="${PASSWORD}" />
            <entry key="transaction.factory_class" value="org.hibernate.transaction.JDBCTransactionFactory" />
        </map>
    <property>
 </bean>

Free advice instead of using the internal hibernate connection stuff (which isn't adviced to be used in production) configure a datasource in spring and wire that to your LocalSessionFactoryBean

like image 73
M. Deinum Avatar answered Oct 13 '22 12:10

M. Deinum