Youtube Channel

Hibernate

Hibernate Framework

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.
An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.
hibernate_orm
The ORM tool internally uses the JDBC API to interact with the database
Advantages of Hibernate Framework

There are many advantages of Hibernate Framework. They are as follows:
1) Opensource and Lightweight: Hibernate framework is opensource under the LGPL license and lightweight.
2) Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled by default.
3) Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don’t need to write database specific queries. Before Hibernate, If database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.
4) Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.
5) Simplifies complex join: To fetch data form multiple tables is easy in hibernate framework.
6) Provides query statistics and database status: Hibernate supports Query cache and provide statistics about query and database status.
Some important terminology related to Hibernate
  • POJO
  • Factory Design Pattern
  • XML  and DTD
  • Database Configuration Properties


Elements Of Hibernate Architecture
SessionFactory
The SessionFactory is a factory of session and client of ConnectionProvider. It holds the second level cache of data. The org.hibernate.SessionFactory interface provides factory method to get the object of Session.
Session               
The Session object provides an interface between the application and data stored in the database. It is a short lived object and wraps the JDBC connection. It is a factory of Transaction, Query and Criteria.
It holds a first level cache of data. The org.hibernate.Session interface provides method to insert, update and delete the object. It also provides factory methods for Transaction, Query and Criteria.
Transaction
The truncation object specifies the atomic unit of work. It is optional. The org.hibernate.Tranaction interface provides method for transaction management.
ConnectionProvider
It is a factory of JDBC connections. It abstracts the application from DriverManager or DataSource . It is optional.
TransactionFactory
It is factory of Transaction. It is optional.

Hibernate Configuration

Hibernate requires to know your database tables and classes related to it .Also configuration settings related to database and other related parameters. All such information is usually supplied as standard properties file called hibernate.properties  or as an XML file named hibernate.cfg.xml
Programatic Configuration
An instance of org.hibernate.cfg.Configuration represents an entire set of mappings of an applications java type to an SQL database. Ther org.hibernate.cfg.Configuration is used to build an immutable org.hibernate.SessionFactory. the mappings are compiled from various XML mapping files.
You can obtain a org.hibernate.cfg.Configuraiton instance by instantiating it directly and specifying XML mapping documents.
Configuration cfg=new Configuration();
Cfg.configure(“Item.hbm.xml”);

Obtaining a SessionFactory
SessionFactory factory=cfg.buildSessionFactory();
JDBC connections
It is advisable to have org.hibernate.SessionFactory creae and pool JDBC connections for you. If you take this approach, opening a org.hibernate.Session is as simple as
Session session=factory.openSession();//open a new Session
Hibernate obtain and pool connections usning java.sql.DriverManager by using following properties
Property name
Purpose
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 polled connections

Hibernate Configuration Properties
Property name
purpose
Hibernate.dialect
The classname of Hibernate
Org.hibernate.dialect.Dialect which allows hibernate t o generate SQL optimized for a particular relational databse
Hibernate.show_sql
Write all sql statements on console.

Hibernate SQL Dialects
RDBMS
Dialect
DB2
Org.hibernate.dialect.DB2Dialect
Oracle
Org.hibernate.dialect.OracleDialect
Ingress
Org.hibernate.dialect.IngresDialect
.
.
.
.
.
.
.
.






XML configuration file
Example

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
<hibernate-configuration>
<session-factory>
<property name="connection.diver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">
jdbc:oracle:thin:@localhost:1521"xe</property>
<property name="connection.username">system</property>
<property name="connection.password">abc</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.OracleDialcet</property>
<property name=hbm2dll.auto>update</property>
<mapping resource="hibernate.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
      
Note: you can save your configuration file with name as ‘hibernate.cfg.xml  ‘ or you can save with anyname but extension should be like anyname.cfg.xml.
In case of other name it is compulsory to pass the fully qualified name to  configure().so that configuration file will be easily found.
Example
Configuration cfg=new Configuration();
Cfg.configure(“anyname.cfg.xml”);

0 comments: