- Pass an instance of
java.util.Properties
toConfiguration.setProperties()
- Set system properties using
java -Dproperty=value
- Place a file called
hibernate.properties
in the classpath - Include
<property>
elements inhibernate.cfg.xml
in the classpath
The first and second options are rarely used except for quick testing and prototypes, but most applications need a fixed configuration file. Both the hibernate.properties
and the hibernate.cfg.xml
files provide the same function: to configure Hibernate. Which file you choose to use depends on your syntax preference. It's even possible to mix both options and have different settings for development and deployment
A rarely used alternative option is to allow the application to provide a JDBC Connection
when it opens a Hibernate Session
from the SessionFactory
(for example, by calling sessions.openSession(myConnection)
). Using this option means that you don't have to specify any database connection properties. We don't recommend this approach for new applications that can be configured to use the environment's database connection infrastructure (for example, a JDBC connection pool or an application server datasource).
This story, "Get started with Hibernate" was originally published by JavaWorld.