Just put a hibernate.properties file next to your hibernate.cfg.xml file. When the SessionFactory is initialized, Hibernate will first search for a file "hibernate.properties" under the root of the classpath. After that, it searches for a "hibernate.cfg.xml" to configure the SessionFactory.
When you don't specify hibernate.connection.* properties in your .XML file, they will be taken from your .properties file. You can override the .properties connection properties in your .xml file by specifying them there with other values.
The names of the files have to be exactly hibernate.properties and hibernate.cfg.xml unless you specify which .properties file to use in your hibernate.cfg.xml (<property name="properties">myownhibernate.properties</property>) or which .xml file to use programmaticaly (SessionFactory sf = new Configuration().configure("myownhibernate.cfg.xml").buildSessionFactory();).
Something like this.
hibernate.properties:
Code:
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class=org.hsqldb.jdbcDriver
hibernate.connection.username=sa
hibernate.connection.password=
hibernate.connection.url jdbc:hsqldb:test
hibernate.cfg.xml:
Code:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
<class-cache
class="org.hibernate.test.legacy.Simple"
region="Simple"
usage="read-write"/>
</session-factory>
</hibernate-configuration>
You can find examples in the Hibernate distribution under the folder "hibernate-distribution-3.X.X.GA\project\etc"
and at
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html