Hibernate Configuration files

Learn about the Hibernate Configuration files in the Hibernate ORM based project. Hibernate framework uses the hibernate.cfg.xml for configuring the ORM environment. Here you will get complete details about the Hibernate Configuration files.

Hibernate Configuration files

Hibernate Configuration files - Learn about the files in the Hibernate project

The Hibernate framework uses the hibernate.cfg.xml and other optional files for configuring the ORM runtime. This article is discussing about the configuration files of Hibernate. As a beginner you should be aware of all these files. You will be dealing with all these file while developing your Hibernate based applications.

Following is the list of configuration files in Hibernate based project:

  • hibernate.cfg.xml: This file is currently used by the developers for providing ORM related configuration.
     
  • hibernate.properties: In the older version of Hibernate this file was used for configuring ORM parameters. But now a days developers are using hibernate.cfg.xml file.
     
  • <pojo-table-mapping>.xml: This file is used to map the POJO class with the table and it's field. After introduction of JPA annotations developers are rarely using this file.

Hibernate configuration file hibernate.cfg.xml

The  hibernate.cfg.xml  is xml file used to provide the variables necessary for configuring the Hibernate ORM runtime. In most of the cases the default value is ok for a general project. The things you will have to change is the dialect, database url, database username and database password.

Here is a simple example of the file hibernate.cfg.xml:


<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/hibernatetutorial</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Here are the important properties of the file:

  • hibernate.connection.driver_class - The JDBC Driver class
  • hibernate.connection.url - Database URL
  • hibernate.connection.username - Database user name
  • hibernate.connection.password - Password to connect to database
  • hibernate.connection.pool_size - Default connection pool size
  • show_sql - If it is true it will show all the sql generated by Hibernate
  • dialect - Database dialect

The hibernate.cfg.xml should present in the class-path of the project.

We will not discuss about hibernate.properties and <pojo-table-mapping>.xml, since these are old things usually not used in the project.

Check more Hibernate 4.2 tutorials.