Hibernate Configuration File

This section will describe you about the hibernate configuration file.

Ads

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

Example

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

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/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:3306/record
</property>
<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>

</session-factory>
</hibernate-configuration>

Advertisements

Ads

 

We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

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

Advertisements

Ads
 

Discuss: Hibernate Configuration File  

Post your Comment


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

Ads

 

Ads