hibernate1

persist() and replicate() methods in Hibernate

persist(): 

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 […]

hibernate1

getTransaction(), beginTransaction() and 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 […]

hibernate1

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 […]

hibernate1

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:

Output

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

hibernate1

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 […]

hibernate1

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)

Output:

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

Example on isConnected()

Output:

 

hibernate1

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 […]

hibernate1

saveOrUpdate() method in Hibernate Session

saveOrUpdate

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 EmployeeSaveOrUpdateExample.java in dabase

Execute EmployeeSaveOrUpdateExample.java

After […]

hibernate1

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 […]

hibernate1

load() and get() methods of Hibernate Session

Hibernate Session provides two methods to access the object (session.get() & session.load()). Both looks like same but there are differences between load () and get () methods. Let’s have a look at those. load(): load() method throws hibernate.ObjectNotFoundException (which is Un-checked exception) if object not found in cache as well as in database. load() method […]