Hibernate was started in 2001 by Gavin King as an alternative to using EJB2-style entity beans. Its mission back then was to simply offer better persistence capabilities than offered by EJB2 by simplifying the complexities and allowing for missing features. Hibernate used its mapping files and configuration files to achieve its objectives. With the introduction of annotations in java community with JDK 1.5, Hibernate community started working on Hibernate 3, which has support for annotations.
In this post, I will try to detail out more information on hibernate and then will identify the basic steps to use hibernate for our first running example application.
Sections in this post: What is hibernate How hibernate works Hibernate architecture Relation with JPA Writing our first application
What is hibernate
Hibernate is an open source object/relational mapping tool for Java. It provides a framework for mapping an object-oriented domain model to a traditional relational database. Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities and can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC.
Mapping Java classes to database tables is accomplished through the configuration of an XML file or by using Java Annotations. Facilities to arrange one-to-many and many-to-many relationships between classes are provided. In addition to managing associations between objects, Hibernate can also manage reflexive associations where an object has a one-to-many relationship with other instances of its own type.
How hibernate works
Hibernate doesn’t get in your way; nor does it force you to change the way your objects behave. They don’t need to implement any magical interfaces in order to be blessed with the ability to persist. All you have to do to put some annotations telling hibernate that how to use them when mapping them with database. At runtime, hibernate reads these annotations and use this information to build queries to send to some relational database.
There is a simple, intuitive API in Hibernate to perform queries against the objects represented by the database. To change those objects you just interact with them normally in the program, and then tell Hibernate to save the changes. Creating new objects is similarly simple; you just create them in the normal way and tell Hibernate about them using annotations so they can get stored to the database.
Hibernate architecture
The above diagram shows a comprehensive architecture of Hibernate. Here are some definitions of the objects depicted in the diagrams:
Application:
The main application consist of all user written java files and other resources.
Transient Objects:
Instances of persistent classes that are not currently associated with a Session. They may have been instantiated by the application and not yet persisted, or they may have been instantiated by a closed Session.
Persistent objects:
Short-lived, single threaded objects containing persistent state and business function. These can be ordinary Java Beans/POJOs. They are associated with exactly one Session. Once the Session is closed, they will be detached and free to use in any application layer (for example, directly as data transfer objects to and from presentation).
SessionFactory:
A thread safe, immutable cache of compiled mappings for a single database. A factory for Session and a client of ConnectionProvider, SessionFactory can hold an optional (second-level) cache of data that is reusable between transactions at a process, or cluster, level.
Session:
A single-threaded, short-lived object representing a conversation between the application and the persistent store. It wraps a JDBC connection and is a factory for Transaction. Session holds a mandatory first-level cache of persistent objects that are used when navigating the object graph or looking up objects by identifier.
TransactionFactory:
(Optional) A factory for Transaction instances. It is not exposed to the application, but it can be extended and/or implemented by the developer.
ConnectionProvider:
(Optional) A factory for, and pool of, JDBC connections. It abstracts the application from underlying Data source or DriverManager. It is not exposed to application, but it can be extended and/or implemented by the developer.
Transaction:
(Optional) A single-threaded, short-lived object used by the application to specify atomic units of work. It abstracts the application from the underlying JDBC, JTA or CORBA transaction. A Session might span several Transactions in some cases. However, transaction demarcation, either using the underlying API or Transaction, is never optional.
Relation with JPA
JPA (Java Persistence API) is an interface for persistence providers to implement. Hibernate is one such implementation of JPA. You can annotate your classes as much as you would like with JPA annotations, however without an implementation nothing will happen. Think of JPA as the guidelines that must be followed or an interface, while Hibernates JPA implementation is code that meets the API as defined by JPA and provides the under the hood functionality.
When you use hibernate with JPA you are actually using the Hibernate JPA implementation. The benefit of this is that you can swap out hibernates implementation of JPA for another implementation of the JPA specification. When you use straight hibernate your locking into the implementation because other ORMs may use different methods/configurations and annotations, therefore you cannot just switch over to another ORM.
Writing our first application
So, we have got a good overview of : what hibernate is all about. Now, its time to do some practical work. Lets create out first Hello world application. In this application, I have created an Employee class and declared four attributes.
- id
- firstname and
- lastname
I want the id attribute should be generated automatically so that application code does not store a local cahche of employee ids.
So far we targeted what we want to make in our first application. Lets identify the files need to be created.
1) hibernate.cfg.xml // This configuration file will be used to store database connection information and schema level settings.
2) EmployeeEntity.java //This class will be java POJO having hibernate annotations.
3) HibernateUtil.java //This class will have utility methods which will be used for creating session factory and session objects.
4) TestHibernate.java //This class will be used to test our configuration settings and Emplyee entity annotations.
Before moving into code, lets see the project setup and adding maven dependencies which need to added to pom.xml to include all compile time and runtime dependencies.
A) Create a maven project
2) Make project to support eclipse
3) Now import java project to eclipse workspace.
Above steps will create the minimum setup. Now its time to add hibernate dependencies.
Maven dependencies in pom.xml
<dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-commons-annotations</artifactid> <version>3.0.0.ga</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-annotations</artifactid> <version>3.3.0.ga</version> </dependency> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <version>5.1.6</version> </dependency> <dependency> <groupid>antlr</groupid> <artifactid>antlr</artifactid> <version>2.7.6</version> </dependency> <dependency> <groupid>commons-collections</groupid> <artifactid>commons-collections</artifactid> <version>3.1</version> </dependency> <dependency> <groupid>dom4j</groupid> <artifactid>dom4j</artifactid> <version>1.6.1</version> </dependency> <dependency> <groupid>javassist</groupid> <artifactid>javassist</artifactid> <version>3.4.GA</version> </dependency> <dependency> <groupid>javax.transaction</groupid> <artifactid>jta</artifactid> <version>1.1</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-api</artifactid> <version>1.5.6</version> </dependency> <dependency> <groupid>org.slf4j</groupid> <artifactid>slf4j-log4j12</artifactid> <version>1.5.6</version> </dependency>
Please note that we are not using all dependencies in this sample program but they will be used when we start expanding our application.
Adding hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property>
<property name="hibernate.connection.password">lg225295</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="hibernate.test.dto.EmployeeEntity"></mapping>
</session-factory>
</hibernate-configuration>
Do not forget to set correct password before running the application.
Adding EmployeeEntity.java
package hibernate.test.dto;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.annotations.OptimisticLockType;
@Entity
@org.hibernate.annotations.Entity(optimisticLock = OptimisticLockType.ALL)
@Table(name = "Employee", uniqueConstraints = {
@UniqueConstraint(columnNames = "ID"),
@UniqueConstraint(columnNames = "EMAIL") })
public class EmployeeEntity implements Serializable {
private static final long serialVersionUID = -1798070786993154676L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
private Integer employeeId;
@Column(name = "EMAIL", unique = true, nullable = false, length = 100)
private String email;
@Column(name = "FIRST_NAME", unique = false, nullable = false, length = 100)
private String firstName;
@Column(name = "LAST_NAME", unique = false, nullable = false, length = 100)
private String lastName;
// Accessors and mutators for all four fields
}
Adding HibernateUtil.java
package hibernate.test;
import java.io.File;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure(
new File("hibernate.cgf.xml")).buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
Please correct the path of hibernate.cgf.xml.
Testing our code
package hibernate.test;
import hibernate.test.dto.EmployeeEntity;
import org.hibernate.Session;
public class TestHibernate {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
// Add new Employee object
EmployeeEntity emp = new EmployeeEntity();
emp.setEmail("demo-user@mail.com");
emp.setFirstName("demo");
emp.setLastName("user");
session.save(emp);
session.getTransaction().commit();
HibernateUtil.shutdown();
}
}
Above code will create a new table employee in database and insert one row in this table. In logs you can verify the insert statement which got executed.
Hibernate: insert into Employee (EMAIL, FIRST_NAME, LAST_NAME) values (?, ?, ?)
If you face problem in running above application, drop me a comment and i will be glad to discuss the problem with you.
Happy Learning!!






Hello,
thanks for the tutorial.
I had to correct two things to make it work :
- in hibernate.cfg.xml, you have to specify the mapping class, eg :
- in the entity bean, you have to specify dynamicUpdate=true into the annotation Entity :
@org.hibernate.annotations.Entity(optimisticLock = OptimisticLockType.ALL, dynamicUpdate = true)
Posted by guillaume | 17 December, 2012, 7:53 amThanks for pointing out. First is definitely a typo so I have corrected this in post itself. Regarding second, I am not sure. It worked for me without dynamic update.
Anyways, as you have logged your observation here, it will definitely help someone if faced problem.
Posted by Lokesh Gupta | 17 December, 2012, 8:10 am