Organizational Research By

Surprising Reserch Topic

how to include properties from external file to hibernate cfg xml


how to include properties from external file to hibernate cfg xml  using -'spring,hibernate'

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>

    

asked Oct 13, 2015 by deepak gupta
0 votes
36 views



Related Hot Questions

3 Answers

0 votes

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.




    
       
            
            
            
            
        
    
 

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

answered Oct 13, 2015 by suyesh.lokhande
0 votes

You can do it programmatically.

hibernate.cfg.xml should be as following.


    " rel="nofollow" target="_blank">http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    
        
            org.hibernate.transaction.JDBCTransactionFactory
        
    

dbConnection.properties

connection.driver_class=org.postgresql.Driver
connection.username=postgres
connection.password=postgres

And when creating the SessionFactory you can do the following.

Properties dbConnectionProperties = new Properties();
try {
    dbConnectionProperties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("dbConnection.properties"));
} catch(Exception e) {
    // Log
}

SessionFactory sessionFactory = new Configuration().mergeProperties(dbConnectionProperties).configure().buildSessionFactory();
answered Oct 13, 2015 by vijaygupta1980
0 votes

Following this, this and this I came up with the following Maven configuration that replaces/filters placeholders from your hibernate.cfg.xml file with properties from a properties file:


    
        
            org.apache.maven.plugins
            maven-resources-plugin
            2.6
            

                
                    copy-resources
                    
                    validate
                    
                        copy-resources
                    
                    
                        ${basedir}/target/extra-resources
                        
                            
                                src/main/resources
                                true
                            
                        
                    
                
            

        
    

    
    
        src/main/resources/dbConnection.properties
    
    
        
            src/main/resources
            true
            
                *
            
            
                hibernate.cfg.xml
            
        
    

With this configuration, you can run the validate Maven goal to generate the filtered files and see if they are relpaced correctly

This is of course useful if you are using Maven.

answered Oct 13, 2015 by mca.agarwal

...