Hibernate And Spring Data JPA

Sanjay Singh
4 min readNov 28, 2019

Configuration cfg = new Configuration();
SessionFactory sf =cfg.buildSessionFactory();
Session session = sf.openSssion();
Student student = new Student(101,”Boss”,”MZP”);

Transation tx = session.begainTransation();


Configuration cfg = new Configutration();

Configuration object is single threaded (ThredSafe)

configure() is responsible for
identify hibernate-cfg.xml file
Read hibernate configure file
identify all mapping resource or mapping class

SessionFactory sf =cfg.buildSessionFactory();

buildSessionFactory() is responsible for

  • it is responsible for creating SessionFactory Object.
  • once SessionFactory object was crated no need Configuration object.

SessionFactory(Interface ) Multithreaded ,Long lived

you need to create per database SessionFactory Object , when you write multiple database then you need write multiple hibernate file(hibernate-cfg.xml).

Session session = sf.openSssion();

Session (Interface) single threaded(Thread Safe ) ,Short Time.

Session represent period of time where user can do multiple database operation

Insert Object




Update Object



Select Object



Delete object


flush(): Forces the session to flush. It is used to synchronize session data with database. When you call session. flush(), the statements are executed in database but it will not committed.

Hibernate Query Language (HQL)



Transaction Interface in Hibernate

Transation tx = session.begainTransation();

Begin a unit of work and return the associated Transaction object. This method needs to be called if you want to enable transaction, and once your DB interactions are done.

void begin() starts a new transaction.
void commit() ends the unit of work unless we are in FlushMode.NEVER.
void rollback() forces this transaction to rollback.
void setTimeout(int seconds) it sets a transaction timeout for any transaction started by a subsequent call to begin on this instance.
boolean isAlive() checks if the transaction is still alive.
void registerSynchronization(Synchronization s) registers a user synchronization callback for this transaction.
boolean wasCommited() checks if the transaction is commited successfully.
boolean wasRolledBack() checks if the transaction is rolledback successfully.

DDL data definition language

<property name=”hbm2ddl.auto”>create-drop</property>

update: update the schema.(Alter,create)
create: creates the schema, destroying previous data.(DROP ,CREATE)
create-drop: drop the schema when the SessionFactory is closed explicitly, typically when the application is stopped.(Drop-crate-drop)

validate: validate the schema, makes no changes to the database.(Check mapping file and Database table )

OpenSession vs getCurrentSession :

Lets summarise differences between openSession and getCurrentSession in below table

Différence b/w OpenSession vs getCurrentSession

DML (Data Manipulation Language)

Update VS Merge

Spring Data JPA

  • Spring Data JPA is not a JPA provider. It simply “hides” the Java Persistence API (and the JPA provider) behind its repository abstraction.
  • Spring Data provides multiple repository interfaces that are used for different purposes.
Flow of spring Project

First, the Spring Data Commons project provides the following interfaces:

The Repository<T, ID extends Serializable> interface is a marker interface that has two purposes:

It captures the type of the managed entity and the type of the entity’s id.

It helps the Spring container to discover the “concrete” repository interfaces during classpath scanning.

The CrudRepository<T, ID extends Serializable> interface provides CRUD operations for the managed entity.

The PagingAndSortingRepository<T, ID extends Serializable> interface declares the methods that are used to sort and paginate entities that are retrieved from the database.

The QueryDslPredicateExecutor<T> interface is not a “repository interface”. It declares the methods that are used to retrieve entities from the database by using QueryDsl Predicate objects.

Second, the Spring Data JPA project provides the following interfaces:

The JpaRepository<T, ID extends Serializable> interface is a JPA specific repository interface that combines the methods declared by the common repository interfaces behind a single interface.

The JpaSpecificationExecutor<T> interface is not a “repository interface”. It declares the methods that are used to retrieve entities from the database by using Specification<T> objects that use the JPA criteria API.



Sanjay Singh

Java||Spring-Boot|| Micro services Application developer|| INDIA