Hibernate

Hibernate  is popular ORM solutions for Java applications. This tutorial provides step by step instructions for Hibernate applications but for minor differences, could also be used with Kodo. Hibernate provides data query and retrieval facilities that significantly reduce development time. Hibernate lets you develop persistent classes following object-oriented idiom – including association, inheritance, polymorphism, composition, and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API.

It was started in 2001 by Gavin King as an alternative to EJB2 style entity bean. The stable release of Hibernate till July 16, 2014, is hibernate 4.3.6. It is helpful for beginners and experienced persons.


Drawbacks of JDBC:

  1. SQL Queries are Database software dependent queries. JDBC uses SQL Queries so JDBC persistent logic is Database software persistent.
  2. Changing Database software in the middle of project development or production environment is complex process.
  3. All Exceptions in JDBC programming are checked exceptions we must catch and handle those exceptions.
  4. JDBC supplied Middleware services but those are not sufficient not good for industry standard projects.
  5. Middleware services are security transaction JDBC connection pooling and e.t.c., to overcome all the above problems we use o-r mapping persistence logic instead of JDBC persistence logic.
  6. The Parameters of PreparedStatememnt related SQL query allows only positional parameters (?) and they does not allow named parameters.
  7. We can send only Serializable Java objects over the network. JDBC ResultSet object can’t be sent over the network because it is not serializable object.
  8. In JDBC programmer have to Write more common logics along with application specific logics in every JDBC application (Scratch level coding is required).

What is o-r mapping?

  1. A) The process of linking/mapping Java class with Database table, Java class member variables with Database table columns and making objects of that Java class representing Database table records having synchronization between them is called o-r mapping. If any modification done in Java object it reflects in row/record of Database and vice-versa.

Here the word synchronization is not related to Multithreading. To develop o-r mapping persistence logic there is no need of working with SQL queries we just need to work with Java objects that are representing Database table records and we use these objects to perform all persistent operations on the table (CURD Operations). This whole process makes or mapping persistence logic as Database software independent persistence logic.

ORM Softwares                              Vendors                                priority

EJB entity bean components  SUN Microsystems (Oracle)                least

Hibernate                                    SoftTree (Red Hat)                                  1 (90% using)

Ibatis                                           Apache                                                        4

JDO (Java Data Objects)        Adobe                                                        least

Top Link                                     Oracle                                                        least

OJB (Object java Bean)           Apache                                                        3

JPA                                              SUN Microsystem (Oracle)                      2


Hibernate Def: Hibernate is open source lightweight and Java based ORM software to develop object based Database software independent o-r mapping persistence logic in Java, Jee and Java Framework software based applications.

  1. When hibernate software is installed it gives its source code to programmers and moreover Hibernate is free software due to this we say Hibernate is opensource software.
  2. EJB Components are heavy weight components because they need heavy weight container, server softwares for execution.
  3. The resources of EJB components are EJB API dependencies. Hibernate software and Hibernate components are light weight because to execute Hibernate applications we use just JDK software, Hibernate Software and there is no need of working with the heavy weight containers, server softwares.
  4. Some resources of Hibernate applications can be developed without using Hibernate API.
  5. We can write Hibernate logic in any kind of Java applications to make them taking with Database Software.

Framework: Framework is special software that is built on the top of core technologies having the ability to generate the common logics of the applications dynamically while working with Framework softwares.

  1. Programmer just develops application specific logics because the Framework software automatically generates the common logics of application dynamically by using core technologies internally.
  2. Framework software provides abstraction layer core technologies internally generate some common logic but they never make programmer bothering or knowing about these core technologies.

EX: Hibernate, Struts, Spring.


Understanding Hibernate abstraction layer on JDBC program:

  1. Register JDBC driver with DriverManager service.
  2. Establish connection with Database software.
  3. Create JDBC statement object.
  4. Send and execute query in Database software.
  5. Gather results and process the results.
  6. Close JBDC objects.
  7. Take care of Exception handling.
  8. Perform Transaction management (if necessary).

1,2,3,6,7 and 8 are common logics because they are always same in JDBC based Java applications.

4,5 are application specific logics because they vary in each JDBC based application.


Hibernate based Java application:

  1. Create a Session factory, session objects.
  2. Perform persistence operations on Database table using objects.
  3. Gather and process the results.
  4. Close session factory, session objects.

Here Hibernate software internally uses JDBC code and generates all the above discussed common logics of persistence code. In Java environments three types of Framework softwares. Those are

  1. Web Framework softwares
  2. ORM Framework Softwares
  3. Java-jee Framework softwares

 

  1. Web Framework softwares: Web Framework softwares provides abstraction layer on Servlets, JSP, Core technologies and simplifies developing of web applications.

EX: Struts, JSF, Web services, Tapstrey, Spring web MVC.

  1. ORM Framework Softwares: These softwares provides abstraction layer on the core JDBC technology. Simplifies the process of developing the persistence logic in o-r mapping style.

EX: Hibernate, OJB, Ibatis, Toplink e.t.c.,

  1. Java-jee Framework softwares: These softwares provides abstraction layer of Java, Jee, Core technologies, JDBC, RMI, JNDI, Servlets, JSP, JMS, Java mail, EJB e.t.c., allows to develop all kinds of Java jee applications Framework style.

EX: Spring


Features of Hibernate:

  1. Light weight ORM Framework Software.
  2. Allows us to Database software independent persistence(o-r mapping persistence logic)
  3. Supports POJO and POJI model programming.
  4. Can be used along with Java, JEE, Java Framework software applications to make them internally with ORM software.
  5. Gives built in JDBC Connection pool also allows to work with third party supplied and server managed JDBC Connection pool.
  6. Gives built in transaction management support and also allow working with server managed Transaction management.

Note: Hibernate gives built-in middleware service and also allowed to work with third party or server supplied middleware services.

Note: Middleware services are additional and optional services which can be applied to our application to make our application running smoothly in all sitchuations.

EX: Transaction Management, Security, JDBC Connection Pooling and e.t.c.,

  1. Gives the records of the table directly in the form of Collection Framework data structures which are serializable objects by default.
  2. Gives the Database software independent query language called HQL.
  3. Also allows using direct SQL queries to develop persistence logic.
  4. Throws only un checked exceptions so Exception Handling while developing Hibernate persistence logic is optional.

Note: Even though Hibernate uses JDBC and even though JDBC code throws Checked exceptions the Hibernate software converts them to unchecked exceptions by using Exception Rethrowing concept.

 

  1. Allows us to develop PL/SQL procedures/ functions of Database software.
  2. Allows to build object based relationship one to one, one to many and e.t.c.,
  3. Gives annotations support in programming as alternate to XML files.

Note: Annotations are Java statements that are alternate for the XML file resources configuration and other applications

  1. Also supports JPA standards of o-r mapping persistence logic development.

Note: JPA defines set of rules and guidelines that are required to develop ORM softwares due to this programmers can work with all these ORM Softwares in a common way.

  1. Easy to communicate and apply.
  2. Supports two levels catching and buffering.

Note: Buffering in Hibernate based client application reduces the network round trips between client and server.

 

JDBC Hibernate
1.      Core technology for developing persistence logic.

2.      Develops SQL queries based persistence logic.

3.      Persistence logic is Database software dependent.

4.      Given by SUN Microsystem.

5.      Internally uses streams.

6.      Talk with Database software directly.

7.      Suitable for medium scale applications.

8.      Lazy loading not possible when we call session.

1.      Framework to develop persistence logic

2.      Develops object based persistence logic(or mapping).

3.      Persistence logic is Database software independent.

4.      Given by SoftTree (Red Hat)

5.      Internally use JDBC.

6.      Talks with Database software by generating JDBC code.

7.      Suitable for large scale applications.

8.      Does not support lazy loading.


Client applications of Hibernate maintain HB POJO calss object(Hibernate persistence class object) in there states.

  1. Transient State
  2. Persistent State
  3. Detached State

The logical memory in client application where all persistent state objects are there is called as persistence context.

 

Transient State:

  • Does not contain identity value.
  • Does not represent Database table.
  • This state will be there for HB Pojo class object before entering persistence context.
  • When programmer creates explicit object for HB POJO class its default state is transient.

Persistence State:

  • Resides inside the persistence context contains identity value represent Database table records having synchronization.
  • In Hibernate programming the total or mapping persistence logic will be written based on this state objet.

Detached State:

  1. When persistence context is closed or Database software is down or Database table records is deleted then persistence state object of persistence context becomes detached state object.
  2. This object contains identity value but does not represent Database table record while developing persistence logic of hibernate in our client applications we need to write two important objects. Those are Hibernate SessionFactory Object, Hibernate SessionObject.

Understanding SessionFactory and Sessionstate object:

Session Factory: It will be created based on the entries of Hibernate configuration file by using org.hibernate.cfg.Configuration class object.

  1. This object is generally on one per Database represents JDBC Connection pool that is there in Hibernate software based Hibernate Configuration file entries. This object also represents other resources like caches
  2. SessionFactory object is the object of Java class that implements org.hibernate.SessionFactory(I).
  3. This object is capable of creating one or more Hibernate session objects

Hibernate Session object: The SessionFactory object takes one JDBC connection object from its JDBC Connection pool and gets one JDBC statement object based on that connection object and also creates Hibernate Session object by encapsulating JDBC Connection, Statement objects.

  1. In One application there can be one or more Hibernate Session objects. Hibernate Session object represent persistence context. Hibernate Session Object provides API and environment to develop object based o-r mapping persistence logic.
  2. Each Hibernate Session object represents Connectivity with Database software.
  3. It is mediator between Java application and Hibernate Software. That means Java application gives instruction to Hibernate software using this Session object.

Open Session(): Makes SessionFactory object to create one Session object and this Session object represent an empty persistent context by default. Since we activate hibernate software in any environment by just creating object for configuration class(Predefiend). So we can say Hibernate is light weight software


Following link to provide the Hibernate Example Tutorials :  https://github.com/rahulmoundekar/HibernateTutorials