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

I need to be able to save database configuration properties in src|main|java|dbConnection.propertiesand include it in jstlhibernate.cfg.xml expressions . (for example: $ {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>
+2
source share
3 answers

You are claiming to be using Spring, why not let Spring do all the hard work. Let the 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>

( ) Spring LocalSessionFactoryBean

+6

.

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="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
        </session-factory>
    </hibernate-configuration>

dbConnection.properties

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

SessionFactory .

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

SessionFactory sessionFactory = new Configuration().mergeProperties(dbConnectionProperties).configure().buildSessionFactory();
+6

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

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
            <executions>

                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/target/extra-resources</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/main/resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>

        </plugin>
    </plugins>

    <!-- Specify the file that contains the value to replace the placeholders -->
    <filters>
        <filter>src/main/resources/dbConnection.properties</filter>
    </filters>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>*</exclude>
            </excludes>
            <includes>
                <include>hibernate.cfg.xml</include>
            </includes>
        </resource>
    </resources>
</build>

With this configuration, you can run Maven validation to create filtered files and verify that they are fixed correctly.

This is of course useful if you are using Maven.

+3
source

Source: https://habr.com/ru/post/1017035/


All Articles