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>
cfg. xml . This file can be used as a replacement for the hibernate. properties file or, if both are present, to override properties.
Let us create hibernate. cfg. xml configuration file and place it in the root of your application's classpath.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With