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

Hibernate config

In this section, you will learn about Hibernate config or configuration in Hibernate.

Ads

Tutorials   
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 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example 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 beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

Hibernate config

In this section, you will learn about Hibernate config or configuration in Hibernate.

The configuration of Hibernate is handled by the instance of org.hibernate.cfg.Configuration . It symbolizes the complete mapping between Java data types to the SQL database or database table. It is used to construct org.hibernate.SessionFactory . Various XML mapping files or Java Annotations compilation build these mapping.

Following 3 types of configuration is provided by the Hibernate :

(1) hibernate.cfg.xml

To define a Hibernate Configuration info a resource file named hibernate.cfg.xml is used. Using this configuration file various of information is provided to the Hibernate such as :

  • connection.driver_class : This property element is used to provide the name of the driver class.
  • connection.url : This property element is used to provide the url for the database connection.
  • connection.username : This property element is used to provide the username by which you are logging to the database.
  • connection.password : This property element is used to provide the password by which you are logging to the databse.
  • connection.pool_size : This property element configures the number of connections of connection pool of the built-in Hibernate.
  • dialect : This property defines the particular SQL variant with which the Hibernate will carry on a conversation. Use of this property is necessary when there are multiple databases are targeted by an application.

At last of you will be required to map the persistent class(POJO) file to the configuration.A simple configuration file hibernate.cfg.xml can be written as :

<?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://192.168.10.13:3306/ankdb</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>

<mapping class="worker.Worker" />

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

(2) hibernate.properties

This property file contains the key-value pair for different hibernate configuration.

The sample hibernate.properties file is given below :

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url= jdbc:mysql://192.168.10.13:3306/ankdb
hibernate.connection.username=root
hibernate.connection.password=root
hibernate.connection.pool_size=1
hibernate.transaction.factory_class = \
org.hibernate.transaction.JTATransactionFactory
hibernate.transaction.manager_lookup_class = \
org.hibernate.transaction.JBossTransactionManagerLookup
hibernate.dialect = org.hibernate.dialect.MySQLDialect

(3) Programmatic configuration

This is the manual configuration which needs to be done in Java file. We can get an instance of org.hibernate.cfg.Configuration directly by specifying mapped class using  .addClass( ).

Configuration cfg = new Configuration()
	.addClass(worker.Worker.class)
	.addClass(worker.Department.class);

Building a SessionFactory

After creating instance of org.hibernate.cfg.Configuration , you can create instance of SessionFactory as follows:

SessionFactory sessions = cfg.buildSessionFactory();

Creating Session instance

 Between database table and application, Session represents a communication channel. From SessionFactory , Session can be created as follows:

Session session = sessions.openSession();

Advertisements

Online Training Classes

Share on Google+Share on Google+

Hibernate config

Posted on: April 16, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Hibernate config  

Post your Comment


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

Ads

 

Ads