Home » Hibernate » Hibernate Configuration File

Hibernate Configuration File

  • DataBase connection details: Driver class, URL, username and Password.
  • Hibernate properties: Dialect, show_sql, second_level_cache .etc
  • Mapping files names.

There must be one configuration file for each database used in the application, suppose if we want to connect with 2 databases, like Oracle, MySql, then we must create 2 configuration files with different names, like oracle.cfg.xml fr Oracle DB and mysql.cfg.xml for mysql database.

Number of database’s = that many number of configuration files.


Sample Configuration File :

hibernate.cfg.xml


<hibernate-configuration>
<session-factory> 
 
<! -- Related to the connection START -->
<property name="connection.driver_class">Driver Class Name </property>
<property name="connection.url">URL </property>
<property name="connection.user">USER NAME </property>
<property name="connection.password">PASSWORD</property>
<! -- Related to the connection END -->
 
<! -- Related to hibernate properties START -->
<property name="show_sql">true/false</property>
<property name="format_sql">true/false</property>
<property name="use_sql_comments">true/false</property>
<property name="dialet">Database dialet class</property>
<property name="hbm2ddl.auto">create/create-drop/update/validate</property>
<! -- Related to hibernate properties END-->
 
<! -- Related to mapping START-->
<mapping resource="hbm file 1 name .xml" / >
<mapping resource="hbm file 2 name .xml" / >
<! -- Related to the mapping END --> 
 
</session-factory>
</hibernate-configuration>

Description of the elements in configuration file

1. connection.pool_size : Used to configure connection pooling in hibernate.cfg.xml.

<property name="connection.pool_size">10</property>

2. show_sql : If the value is true, We can see generated sql statements in console..

<property name="show_sql">true</property>

3. format_sql : If the value is true, We can see generated sql statements in a readable format.

<property name="format_sql">true</property>

4. use_sql_comments : If the value is true, We can see comments in generated sql statements.

<property name="use_sql_comments">true</property>

5. hbm2ddlauto :

  1. create: Creates schema, destroys previous data.
  2. create-drop: Drops the schema at the end of a session.
  3. update: Updates the schema.
  4. validate:Validates the schema. It makes no changes to database.


Important Note:

In real time projects, developers will not define database connection details in hibernate.cfg.file.Instead we need to use JNDI name of data source for db connection. Steps to create JNDI Data source depends on server which is used to deploy the application. For example, if we want to use JNDI data source using Tomcat, the configuration will be as below.

server.xml entry

Add below code in the tomcat server.xml file present in C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf\server.xml.

<Resource name="jdbc/MyTestDB"
      global="jdbc/MyTestDB"
      auth="Container"
      type="javax.sql.DataSource"
      driverClassName="oracle.jdbc.driver.OracleDriver"
      url="jdbc:oracle:thin:@localhost:1521/TEST"
      username="system"
      password="mukesh"       
      maxActive="100"
      maxIdle="20"
      minIdle="5"
      maxWait="10000"/>

Resource Link Configuration – context.xml entry

<ResourceLink name="jdbc/MyTestDB"
              global="jdbc/MyTestDB"
              auth="Container"
              type="javax.sql.DataSource" /> 


Hibernate.cfg.xml

<property name="hibernate.connection.datasource">java:comp/env/jdbc/MyTestDB</property>


Previous Next Article

comments powered by Disqus