Hibernate
Hibernate
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.
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()
● session.detach(e);
● session.evict(e);
● session.clear();
● session.close();
Example
// Transient State
// Persistent State
session.save(e);
// Detached State
session.close();
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
// Transient State
Session s = sessionfactory.openSession();
e.setId(01);
// Persistent State
session.save(e)
// Removed State
session.delete(e);