Pages

Hibernate

Hibernate
Hibernate Definition

Hibernate is an Object Relational Mapping (ORM) Tool for Java Technology. Hibernate is an open source persistent framework created by Gavin King in 2001. Hibernate is a powerful, high performance Object Relational Persistence and Query Service for any Java Application. Hibernate sits between traditional Java objects and database server to handle all the works in persisting those objects based on the appropriate O/R mechanisms and patterns. Hibernate maps Java classes to database tables and from Java data types to SQL data types and relieves the developer from 95% of common data persistence related programming tasks.


Configuration

Application configuration settings like Data Base connection details. Entity/Persistent classes etc. are configured by this interface. For the whole application there will be only one Configuration for a perticular Data Base.
Syntax : Configuration config = new Configuration().configure();



Session Factory

A SeesionFactory is a global object, that represent a particular hibernate configuration for particular set of mapping metadata. The delivery of session objects is done by this interface. It creates or opens a session to talk to a database. It is built onece at startup with load on startup servlet. For the whole application, there will be generally one SessionFactory for a particular Configuration and can be shared by all the application threads. The org.hibernate.SessionFactory interface provides factory method to get the object of Session.
Syntax : SessionFactory factory = configure.buildSessionFactory();



Session

A Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object. Session object are not usually thread safe, they should not be kept open for a long time and they should be created and destroyed them as needed. You should always use one session per request or one session per transaction. The main function of the Session is to offer CURD operations for instances of mapped entity classes. The org.hibernate.Session interface provides methods to insert, update and delete the object. It also provies factory methods for Transation, Query and Criteria.
Syntax : Session session = factory.openSession();



Transation

Transation interface that defines the unit of work. It maintains abstraction from the transaction implementation of JTA/JDBC. A Transation is associated with Session. It contains commit() and rollback() methods to main consistency. A Transation represent a unit of work with the database.
Syntax : Transation tx = session.beginTransaction();



Query and Criteria

Both are interfaces used to perform some user defined queries. Query syntax looks closer to SQL language. Criteria syntax is pure object oriented. Criteria objects are used to create and execute object oriented criteria queries to retrieve objects.
Syntax : Query query = session.createQuery("from Employee e where e.empid = 100");
Criteria criteria = session.createCriteria(Employee.class).add(Restrictions.eq("empid",100));



Different Between JPA and Hibernate
JPA
  1. JPA defines the management of relational data in the Java applications.
  2. JPA is just a specification. Various ORM tools implement it for data persistence.
  3. JPA is defined in javax.persistence package.
  4. EntityManagerFactory interface is used to interact with the entity manager factory for the persistence unit.
  5. JPA uses EntityManager interface to create, read, and delete operations for instances of mapped entity classes.
  6. EntityManager interface interacts with the persistence context.
  7. JPA uses JPQL as an object oriented query language to perform database operations.
Hibernate
  1. Hibernate is an ORM Tool which is used to save the state of Java Object into the database.
  2. Hibernate is one of the most frequently used JPA implementation.
  3. Hibernate is defined in org.hibernate package.
  4. Hibernate uses SessionFactory interface to create Session instances.
  5. Hibernate uses Session interface to create, read, and delete operations for instances of mapped entity classes.
  6. Hibernate behaves as a runtime interface between a Java application and Hibernate.
  7. Hibernate uses HQL as an object oriented query language to perform database operations.