Hibernate Configuration File

Hibernate Java - Hibernate Open Source ORM tool in Java What is difference between Hibernate 4 and 5? Hibernate 5.3 features Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError

Hibernate Configuration File

This section will describe you about the hibernate configuration file.

This section will describe you about the hibernate configuration file.

Hibernate Configuration File

In this section we will read about the hibernate configuration file. In this file we will see how we can provide the database connection information to a file.

Hibernate Configuration file i.e. hibernate.cfg.xml file is a file where we can provide the database connectivity information from outside of our class. Using this information hibernate makes the connection between the program and database. We didn't need to write the database connectivity code into our program. This makes the loose coupling between database and the application. Using this feature you can change your database at any time this will not put effect on your application i.e. you will not be required to compile your program again and again when you change your database. In this configuration file you provides the database connectivity information inside the <session-factory></session-factory>, sub tag of <hibernate-configuration> </hibernate-configuration>. In the hibernate configuration file you can also map the Java class and database table's relationship. <mapping></mapping> tag is used to add the hibernate XML mapping file. The hibernate XML file i.e. xxxx.hbm.xml file is a file where the Java class and database table is mapped at one place in this file you can map the class properties with the database table's field. You can also add Hibernate mapping file programmatically using the addResource() method of Configuration class.

Hibernate configuration file provides the information to hibernate in advance that how the Java class and database tables are related to each other. This information can be provided as in the form of the hibernate.properties file or in the hibernate configuration file. The hibernate configuration file contains various properties into which some have their default values and some are required to specify. This file should be kept into the root directory of the application class path.

Properties of Hibernate Configuration File

Hibernate configuration file has the various properties. Some of them are as follows :

  • Hibernate JDBC Properties
    • hibernate.connection.driver_class : JDBC driver class.
    • hibernate.connection.url : JDBC URL
    • hibernate.connection.username : database user
    • hibernate.connection.password : database user password
    • hibernate.connection.pool_size : maximum number of pooled connections
  • Hibernate Datasource Properties
    • hibernate.connection.datasource : datasource JNDI name
    • hibernate.jndi.url : URL of the JNDI provider (optional)
    • hibernate.connection.username : database user (optional)
    • hibernate.connection.password : database user password (optional)

Except the above Hibernate properties there are more properties that are used in the hibernate configuration file. Some of these are as follows :

  • hibernate.dialect : This property provide the facility to hibernate for generating SQL optimized for a particular relational database.
  • hibernate.show_sql : This property is used to show the SQL statement at console.
  • hibernate.connection.release_mode : This property determines the releasing of JDBC connection release i.e. when Hibernate should release JDBC connection.
  • hibernate.transaction.auto_close_session : This property is used to automatically close the session, if its value is set to true,  after the transaction phase is complete.

SQL Dialects

The specified SQL dialects allows the Hibernate to set some of the required default properties of the corresponding database. In this case you will not need to specify those properties explicitly.

Hibernate SQL Dialects are as follows :

RDBMS hibernate.dialect
DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 9i org.hibernate.dialect.Oracle9iDialect
Oracle 10g org.hibernate.dialect.Oracle10gDialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Ingres org.hibernate.dialect.IngresDialect
Progress org.hibernate.dialect.ProgressDialect
Mckoi SQL org.hibernate.dialect.MckoiDialect
Interbase org.hibernate.dialect.InterbaseDialect
Pointbase org.hibernate.dialect.PointbaseDialect
FrontBase org.hibernate.dialect.FrontbaseDialect
Firebird org.hibernate.dialect.FirebirdDialect


Here we will see a simple hibernate configuration file example. In this example we will use the RDBMS "MySQL" so we will use the respective hibernate.dialect to "org.hibernate.dialect.MySQLDialect".


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/record
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</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.current_session_context_class">thread</property>