How can I configure Hibernate with environment variable
One of the ways is to use setProperty(String propertyName, String value) of Configuration to explicitly override the value of hibernate.connection.url
before creating the SessionFactory.
To get the environment variables , you can use System.getenv(String name) .
/**Load the hibernate.cfg.xml from the classpath**/
Configuration cfg = new Configuration();
cfg.setProperty("hibernate.connection.url", System.getenv("DATABASE_URL"));
SessionFactory sessionFactory = cfg.buildSessionFactory();
I searched a lot for another solution without programming anything in java itself. I came to the following conclusion.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.check_nullability">false</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.username">${hibernate_username}</property>
<property name="hibernate.connection.password">${hibernate_password}</property>
<property name="hibernate.connection.url">jdbc:postgresql://${hibernate_db_host}/${hibernate_db_name}</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">${hibernate_show_sql}</property>
</session-factory>
</hibernate-configuration>
And I start my application with following vmargs:
-Dhibernate_username=test -Dhibernate_password=testpassword -Dhibernate_db_host=localhost -Dhibernate_db_name=test -Dhibernate_show_sql=true
I post this solution to this old Post, because I found this in an old forum post (Google Search Side 3+ ^^). And I think this is very useful.
May this help you,
I am using HSQL DB of Jboss AS 5.x and hibernate to dynamically create tables and using following *.cfg.xml file.
which is using $
JBOSS_HOME as environment variable.
<?xml version="1.0" encoding="UTF-8"?>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:$JBOSS_HOME/server/test/data/hypersonic/localDB</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="friends_presence_log.hbm.xml" />
<mapping resource="profileuuid.hbm.xml" />
</session-factory>
So, Its mean if you want to use environment variable into Jboss config then your can use normally which later taken by internal hibernate.jar utility and you get connection or things normally as in a java program.