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:
- SQL Queries are Database software dependent queries. JDBC uses SQL Queries so JDBC persistent logic is Database software persistent.
- Changing Database software in the middle of project development or production environment is complex process.
- All Exceptions in JDBC programming are checked exceptions we must catch and handle those exceptions.
- JDBC supplied Middleware services but those are not sufficient not good for industry standard projects.
- 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.
- The Parameters of PreparedStatememnt related SQL query allows only positional parameters (?) and they does not allow named parameters.
- 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.
- 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?
- 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.
- 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.
- EJB Components are heavy weight components because they need heavy weight container, server softwares for execution.
- 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.
- Some resources of Hibernate applications can be developed without using Hibernate API.
- 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.
- Programmer just develops application specific logics because the Framework software automatically generates the common logics of application dynamically by using core technologies internally.
- 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:
- Register JDBC driver with DriverManager service.
- Establish connection with Database software.
- Create JDBC statement object.
- Send and execute query in Database software.
- Gather results and process the results.
- Close JBDC objects.
- Take care of Exception handling.
- 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:
- Create a Session factory, session objects.
- Perform persistence operations on Database table using objects.
- Gather and process the results.
- 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
- Web Framework softwares
- ORM Framework Softwares
- Java-jee Framework softwares
- 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.
- 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.,
- 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.
Features of Hibernate:
- Light weight ORM Framework Software.
- Allows us to Database software independent persistence(o-r mapping persistence logic)
- Supports POJO and POJI model programming.
- Can be used along with Java, JEE, Java Framework software applications to make them internally with ORM software.
- Gives built in JDBC Connection pool also allows to work with third party supplied and server managed JDBC Connection pool.
- 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.,
- Gives the records of the table directly in the form of Collection Framework data structures which are serializable objects by default.
- Gives the Database software independent query language called HQL.
- Also allows using direct SQL queries to develop persistence logic.
- 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.
public void bml()
------------- source code which may checked exception
Throw new arithmetic exception();// Exception Rethrowing
- Allows us to develop PL/SQL procedures/ functions of Database software.
- Allows to build object based relationship one to one, one to many and e.t.c.,
- 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
- 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.
- Easy to communicate and apply.
- Supports two levels catching and buffering.
Note: Buffering in Hibernate based client application reduces the network round trips between client and server.
|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.
- Transient State
- Persistent State
- Detached State
The logical memory in client application where all persistent state objects are there is called as persistence context.
- 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.
- 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.
- 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.
- 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.
- 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
- SessionFactory object is the object of Java class that implements org.hibernate.SessionFactory(I).
- 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.
- 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.
- Each Hibernate Session object represents Connectivity with Database software.
- 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