Generator classes in Hibernate

Generator classes are used to generate the ‘identifier or primary key value‘ for a persistent object while saving an object in database. Hibernate provides different primary key generator algorithms. All hibernate generator classes implements interface, and overrides the generate(SessionImplementor,Object) method to generate the ‘identifier or primary key value‘. If we want our own user […]

persist() and replicate() methods in Hibernate


It is equivalent to save(object) , but save(object) method returns identifier and persist(object) returns nothing. Use save() method, If you are using generator class to generate the identifier and you want to know the identifier value. Use persist() method, if you don’t want to know the generated identifier. replicate(): Use to move the […]

getTransaction(),beginTransaction(),getIdentifier() in Hibernate

Transaction object can be created by 2 ways

getTransaction(): Returns the Transaction object associated with current session. It returns the same Transaction object for every request.

We need to call begin() method on Transaction object to start the transaction.

beginTransaction(): Returns different transaction objects. For each and every request transaction object creates a~lcl begins new transaction context.

getIdentifier(): Use to […]

merge() method in Hibernate

merge(): It merge the detached object data into persistent data.

Lets understand merge() method by example. Employee table in database


Output in Eclipse

Employee table in database,After execution of above program  

Before 23rd line, emp1 is in Persistent state and emp2 is in detached state. In 23rd line, merge() method […]

refresh() method in Hibernate

refresh(): It is used to synchronize database data with session data.

Lets see the importance of refresh() method with the below scenarios. Scenario 1: One session and calling one time get() method Employee table

Application code:


Explanation session.get(Employee.class, 1) hits the database and fetches the data from database and assign the data […]

flush() method in Hibernate

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. If you dont call session.flush() and if you call session.commit() , internally commit() method executes the statement and commits. So commit()= flush+commit. So seesion.flush() just […]

contains() and isConnected methods in Hibernate

contains(Object object): Used to check whether the instance is associated with the session or not.

Example on contains(object)


isConnected(): Used to check whether session is currently connected or not.

Example on isConnected()



clear(), evict() and close() methods in Hibernate

clear():  Completely clear the session and is used to dissociate/disconnect all the objects from the session.

Example on clear() Database table before execution


Database table after execution

 After calling session.clear(), emp1 and emp2 objects are disconnected from the session object. i.e emp1 and emp2 objects are moved from persistent state to detached […]

saveOrUpdate() method in Hibernate Session


If the record is not present in the database, it will call save() method and inserts the record in the database. If the record is present in the database, it will call update() method and updates the record in the database. Example on saveOrUpdate: Before executing the in dabase


After […]

When update() method has to call?

Transient state? Transient state means, Object is neither associated with Session nor present in Database. When you don’t have record in the database, then you will not be able to update the record. So when the object is transient state you will not call update() method. Persistent state? Persistent state means, Object is associated with […]