Hibernate was started by Gavin King in 2001 with a motive to simplify the persistence capabilities offered by EJB2. It simplified the process by adding the features that were missing in EJB2.
Hibernate is an ORM tool. ORM is the Object-Relational Mapping of tables to the entity classes, rows to the class instances and columns of the table to attributes of the instances.
Hibernate is a persistence framework that makes use of a Hibernate cache to persist the data from the Java environment to the database. The Hibernate cache helps in reducing the number of calls made to the database. Entities (Table values) are stored in the Hibernate cache, which contain the updated values.
Hibernate uses persistent objects (POJO) and XML documents to persist objects to the database.
There are three ways by which we can define the configuration (in order):
- Properties file (hibernate.properties)
- XML configuration file (may be as hibernate.cfg.xml or hibernateContext.xml as possibly in a Spring based application)
Configuration file (hibernate.cfg.xml) looks something like:
<hibernate-configuration>. . .
. . .
<mapping resource="Account.hbm.xml"/> //Example
Assume we are dealing with some Account Object as:
Object Mapping File (Account.hbm.xml) looks something like:
<class name="courses.hibernate.vo.Account" table="ACCOUNT">
<id name="accountId" column="ACCOUNT_ID">
<property name="AccountType" column="ACCOUNT_TYPE" type="String" update="false"></property>
<property name="CreationDate" column="CREATION_DATE" type ="Timestamp" update="false"></property>
<property name="Balance" column="BALANCE" type ="double"></property>
Entities in Hibernate can be in one of four states:
- Transient – A newly created object.
- Persistent – The only state that has impact on the database (auto saved)
- Removed – When delete() is called.
- Detached – When clear()/ evict() is called; when persistent object is referenced after closure of the session.
Session methods do not make changes to the database; rather they SCHEDULE the changes that are to be made. Once the session is flushed (as per the flush mode), the values are updated in the database.
There are 4 modes for Flushing:
- ALWAYS – Flushing is done for every query
- AUTO – Hibernate controls flushing making sure updated values are always available.
- MANUAL – Flushing is specified explicitly.
- COMMIT – Flushing is done on encountering commit statements.
A persistence context is a concept that is a list of SQL statements to be sent to the database. Each Hibernate session has one persistence context.
Once configuration is done, a session factory is required to create a session. While SessionFactory is thread-safe (singleton for the entire application), Session is not. A session in Hibernate represents a conversation between the application and the database, is a factory for Transaction and holds a cache of persistent objects.
SessionFactory sessions = cfg.buildSessionFactory();
Session session = sessions.openSession();
Now we know what Hibernate is, how to configure it, the different Hibernate states as well as the different flushing modes. For any queries, feels free to post them as comments.
Caching in Hibernate – First and second level cache