JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven









Overview of Hibernate


Introduction

  • It is an open source  java based solution.
  • It provides solution using mapping between objects.
  • This mapping is called ORM(Object Relational Mapping).
  • It uses MVC framework.
  • It has model view and controller all the three components.
  • Its components are light weighted.


What is Hibernate?


  • Hibernte is an open-source  framework (mapping tool)
  • It is used to establish connection between database and  java or JEE Application using ORM(Object Relational Mapping) technique.
  • Hibernate takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types)
  • It provides data query and retrieval facilities
  • It significantly reduce development time otherwise spent with manual data handling in SQL and JDBC. 
  • Hibernates goal is to relieve the developer from 95 percent of common data persistence related programming tasks.
  • Hibernate is available for download at http://www.hibernate.org/

Feature
  • Object / Relational mappings
  • Object-oriented query language
  • Automatic primary key generation
  • J2EE integration
  • High performance

  • Object Relational Mapping
  • It is a new solution given in java against ejb whcih is heavy weight components.
  • It has light weight components.
  • It persists data in the database.
  • It provides transaparent solution as the table of the database is hidden.
  • It gives all major three operations called CRUD (Create, Update and Delete) of the database.
  • It provides database support with vendor independence.
  • Any vendor specific database can be used.
  • It has given the solutions for all the major database

  • Object persistance

  • The objects gets  persisted  in the database.
  • It happens  by making bean class  configuration with the database table
    and bean properties with the columns.
  • By the introduction of annotations  in Java 5 the mapping is done in entity bean
    by the annotation in place of the xml file.

The role of Hibernate in a Java application




  • Hibernate makes use of persistent objects commonly called as POJO (POJO = "Plain Old Java Object".) along with XML mapping documents for persisting objects to the database layer.
  •  The term POJO refers to a normal Java objects that does not serve any other special role or implement any special interfaces of any of the Java frameworks (EJB, JDBC, DAO, JDO, etc...).
  • Rather than utilize byte code processing or code generation, Hibernate uses runtime reflection to determine the persistent properties of a class
  • . The objects to be persisted are defined in a mapping document, which serves to describe the persistent fields and associations, as well as any subclasses or proxies of the persistent object
  • The mapping documents are compiled at application startup time and provide the framework with necessary information for a class.
  • they are used in support operations, such as generating the database schema or creating stub Java source files. 

High level architecture of Hibernate 



sessionFactory Instatiation

sessionFactory = new Configuration().configure().buildSessionFactory()
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
Customer newCustomer = new Customer();
newCustomer.setName("New Customer");
newCustomer.setAddress("Address of New Customer");
newCustomer.setEmailId("NewCustomer@NewCustomer.com");
session.save(newCustomer);

tx.commit();

  • First step in hibernate application is to retrieve Hibernate Session
  • Hibernate Session is the main runtime interface between a Java application and Hibernate
  • SessionFactory allows applications to create hibernate session by reading hibernate configurations file hibernate.cfg.xml.
  • After specifying transaction boundaries, application can make use of persistent java objects and use session for persisting to the databases.

Integrating and Configuring Hibernate


Mapping POJO with persistence layer using hibernate mapping document

Each persistent class needs to be mapped with its configuration file. Following code represents Hibernate mapping file for Employee class

POJO Class

This pojo (java bean file ) represents the persistent class.
This will get mapped with the specific table in the database.





Hibernate Mapping File
  • We need to map the POJO file with the Database table.
  • Each class is mapped to the table.
  • Each attribute of the pojo file  is mapped  with the table column.
  • There must be one identifier for in the pojo file.
  • This identifier will be one of the pojo attribute.
  • In mapping file this identifier will be mapped with primary key column.
  • This primary key column might be using sequence or auto increment.
  • This is generally saved as hibernate.hbm.xml.





  • Hibernate mapping documents are straight forward.
  • <class> element maps a table with corresponding class
  • The <id> element represents the primary key column, and its associated attribute in the domain object
  • The <property> elements represent all other attributes available in the domain object.


Utility.java
  • Hibernate SessionFactory are thread safe.
  • So it is recommended that there should be only copy of  Sessionfactory.
  • But Hibernate Session are not thread safe.
  • So Session can have differrent instances.
  • This can be achieved by the use of Singleton.
  • We will create static instance of Sessionfactory
  • Then create static method returning SessionFactory instace.


Hibernate Configuration File

  • Hibernate configuration file information needed to connect to persistent layer and the linked mapping documents.
  • The element <mapping-resource> refers to the mapping document that contains mapping for domain object and hibernate mapping document.
  • One can either specify the data source name or JDBC details that are required for hibernate to make JDBC connection to the database.
  • Inside session-factory tag we will provide the database configuration
  • This is done with the help of property tag.
  • Property tag is having differrent name and values.
  • This is  generally saved as hibernate.cfg.xml
  • These properties are
    • hibernate.connection.driver_class
    • hibernate.connection.url
    • hibernate.connection.username
    • hibernate.connection.password
    • hibernate.dialect
Example
In this example we will create one simple application step by step.

  • Hibernate can be connected with differrent databases
  • It uses dialect for connecting with mysql.
  • In this example we will use mysql dialect to connect mysql database.

mysql\bin>mysql -u root -p
Enter password: *****

mysql> use test;
Database changed

mysql> create table stud (roll int primary key,  name char(12));
Query OK, 0 rows affected (0.12 sec)

mysql> desc stud;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| roll  | int(11)  | NO   | PRI |         |       |
| name  | char(12) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)

Make a bean file stud.java