0% found this document useful (0 votes)
22 views

Hibernate

Uploaded by

sriramgadde754
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Hibernate

Uploaded by

sriramgadde754
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Hibernate core dependency use to get hibernate feature

Standard file name hibernate.cfg.xml create under src/main/java


If we use the standard name then compiler will automatically read the file and connected with
database and give the object of sessionFactory()
Otherwise we have to give the path of the file name.

Java based configuration


Save

Fetch data from database load() and get()


Get method first check the data present in cache if not then only it will bring the data from db. If
data is not found it returns null. It is called eager loading.

Similarly load will bring the data but it will return a proxy object .Unless that object is used it will
not fetch the query .As soon as we use the object query will execute and bring the data from
cache or db.load using lazy loading concept. It throw objectnotfoundexception if data is not
present so it increase the performance
Query is not executed because emp and emp2 are not used .

Caching
Use hibernate eh cache dependency and add this property

1. save() method: This method is used to persist a new entity object into the
database. If the entity already exists in the database, it will throw an exception. If
the entity doesn't have an identifier, Hibernate will generate one for it.
2. update() method: This method is used to update an existing entity object in the
database. It throws an exception if the entity is not in the persistent state.
3. saveOrUpdate() method: This method is used to either save a new entity or
update an existing entity. If the entity exists, it will be updated; otherwise, a new
entity will be saved.
4. persist() method: This method is used to persist a new entity object into the
database. If the entity already exists in the database, it will not throw an
exception, but instead, it will ignore the call.

State 1: Transient State


The transient state is the first state of an entity object. When we instantiate an object
of a POJO class using the new operator then the object is in the transient state. This
object is not connected with any hibernate session. As it is not connected to any
Hibernate Session, So this state is not connected to any database table. So, if we make
any changes in the data of the POJO Class then the database table is not altered.
Transient objects are independent of Hibernate, and they exist in the heap memory.
Example:
//Here, The object arrives in the transient state.
Employee e = new Employee();
e.setId(21);
e.setFirstName("Neha");
e.setMiddleName("Shri");
e.setLastName("Rudra");
State 2: Persistent State

Once the object is connected with the Hibernate Session then the object moves into
the Persistent State. So, there are two ways to convert the Transient State to the
Persistent State :
In this state. each object represents one row in the database table. Therefore, if we
make any changes in the data then hibernate will detect these changes and make
changes in the database table.
Following are the methods given for the persistent state:

● session.persist(e);

● session.save(e);

● session.saveOrUpdate(e);

● session.update(e);

● session.merge(e);

● session.lock(e);

Example:

// Transient State
Employee e = new Employee("Neha Shri Rudra", 21, 180103);
//Persistent State
session.save(e);
State 3: Detached State

For converting an object from Persistent State to Detached State, we either have to
close the session or we have to clear its cache. As the session is closed here or the
cache is cleared, then any changes made to the data will not affect the database table.
Whenever needed, the detached object can be reconnected to a new hibernate
session. To reconnect the detached object to a new hibernate session, we will use the
following methods as follows:

● merge()

● update()

● load()

● refresh()

● save()
● update()

Following are the methods used for the detached state :

● session.detach(e);

● session.evict(e);

● session.clear();

● session.close();

Example

// Transient State

Employee e = new Employee("Neha Shri Rudra", 21, 180103);

// Persistent State

session.save(e);

// Detached State

session.close();

State 4: Removed State

In the hibernate lifecycle it is the last state. In the removed state, when the entity

object is deleted from the database then the entity object is known to be in the
removed state. It is done by calling the delete() operation. As the entity object is in the

removed state, if any change will be done in the data will not affect the database table.

Example

// Java Pseudo code to Illustrate Remove State

// Transient State

Employee e = new Employee();

Session s = sessionfactory.openSession();

e.setId(01);

// Persistent State

session.save(e)

// Removed State

session.delete(e);

You might also like