I need to centralize all settings for our Java web application in one .properties file. I can still have hibernate.cfg.xml for adding mappings to entity classes but I need to keep all of our settings for the database and custom paths in one .properties file.
Originally I kept my configs in hibernate.cfg.xml as follows….
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">my jdbc connection</property>
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.current_session_context_class">managed</property>
<mapping class="myEntityClass"/>
</session-factory>
</hibernate-configuration>
Now I want to move”connection.url”, “connection.username”, and “connection.password” to my own .properties file. The code for creating my hibernate configuration class went from.
new AnnotationConfiguration().configure();
to
new AnnotationConfiguration()
.setProperty("connection.url", databaseUrl)
.setProperty("connection.username", databaseUser)
.setProperty("connection.password", databasePassword)
.configure();
Which seemed conceptually simple. Unfortunately I get the following error when I try to use my Hibernate Session that worked with the previous config.
The user must supply a JDBC connection
Any ideas? It seems to me that when Hibernate sees these properties missing in the hibernate.cfg.xml file it assumes all settings will be manually added and ignore the xml altogether.
From the Hibernate Reference Documentation:
Adapt it to suit your needs and put the
hibernate.propertiesin the root of the class path (and remove the equivalent entries from thehibernate.cfg.xmlas the XML configuration file overrides properties). So there is actually no need to change the following line:Unless you really want a programmatic configuration of course.
But from the body of your question, moving to a
.propertiesfile is something else and you can rely on Hibernate: move the relevant properties fromhibernate.cfg.xmltohibernate.properties.