Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Hibernate Configuration File

This section will describe you about the hibernate configuration file.


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 How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials

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 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>



Share on Google+Share on Google+

Hibernate Configuration File

Posted on: July 10, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Hibernate Configuration File  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image