Session class in Hibernate

Session class

  • The Session interface is the main interface that Hibernate provides to the application for manipulating the database. It provides the basic methods of saving, updating, deleting and loading Java objects. These methods are completed by its implementation class SessionImpl.
  • The Session has a cache, and the objects in the cache are called persistent objects, which correspond to the relevant records in the database. The Session can execute related SQL statements according to the changes of the objects in the cache at certain points in time to update the database synchronously , this process is called flushing.
  • From the perspective of persistence, Hibernate divides objects into four states: persistent state, temporary state, free state, and deleted state. Session specific methods can make objects transition from one state to another.

Session cache

  • The implementation of the Session interface contains a series of Java collections, which constitute the Session cache. As long as the Session instance does not end its life cycle and the cache is not cleaned up, the objects stored in its cache will not end its life cycle.
  • Session caching reduces the frequency with which Hibernate applications access the database.

flush cache

flush: Session updates the database synchronously according to the property changes of the objects in the cache.
By default , the Session refreshes the cache at the following time points:
1. Explicitly call the flush() method of the Session

2. When the application calls the commit() method of Transaction, the method flushes first, and then submits the transaction to the database

3. When the application performs some query (HQL, Criteria) operations, if the attributes of the persistent object in the cache have changed, the cache will be flushed first to ensure that the query results can reflect the latest state of the persistent object

Exceptions to flush cache
If the object uses the native generator to generate the OID, then when the save() method of the Session is called to save the object, the insert statement to insert the entity into the database will be executed immediately.

The difference between the commit() and flush() methods
flush executes a series of sql statements, but does not commit the transaction; the commit method calls the flush() method first, and then commits the transaction. It means that committing the transaction means that the database operation is permanently saved.

Hibernate primary key generation strategy

write picture description here

Set the time to refresh the cache

If you want to change the default time point of flush, you can explicitly set the time point of flush through the setFlushMode() method of Session
write picture description here

Persistent object state

From the perspective of persistence, Hibernate divides objects into four states: persistent state, temporary state, free state, and deleted state. Session specific methods can make objects transition from one state to another.

Transient

In the case of using a surrogate primary key, the OID is usually null
and not in the Session's cache and there is no corresponding record
in the database

Persistent objects (also called "managed") (Persist):

The OID is not null
and is located in the Session cache.
If there is already a corresponding record in the database, the persistent object and the relevant records in the database correspond to the
Session. When flushing the cache, the database will
be In the cache of the same Session instance, each record in the database table only corresponds to a unique persistent object

Removed object (Removed)

There is no record corresponding to its OID in the database It is no
longer in the Session cache In
general, the application should not use the deleted object anymore

Detached objects (also called "detached"):

OID is not null and is
no longer in the Session cache. Under
normal circumstances, free objects are converted from persistent objects, so there may be records corresponding to them in the database.

object transformation graph
write picture description here

Session's save() method

Session's save() method turns a temporary object into a persistent object

The save() method of Session completes the following operations:
1. Add the News object to the Session cache to make it into a persistent state

2. Use the identifier generator specified in the mapping file to assign a unique OID to the persistent object. In the case of using a surrogate primary key, the setId() method sets the OID for the News object to be invalid.

3. Plan to execute an insert statement: Hibernate maintains the corresponding relationship between it and database related records by persisting the OID of the object when flushing the cache
. When the News object is in a persistent state, the program is not allowed to modify its ID at will

The difference between persist() and save():
when the save() method is executed on an object whose OID is not Null, the object will be saved to the database as a new oid; but when the persist() method is executed, a abnormal.

Session's get() and load() methods

Both can load a persistent object from the database according to the specified OID
Difference :

  • When the record corresponding to the OID does not exist in the database, the load() method throws ObjectNotFoundException, and the get() method returns null
  • The two use different lazy retrieval strategies: the load method supports lazy loading strategies. And get does not support.

Session's update() method

  • The Session's update() method makes a detached object persistent and schedules an update statement to execute.

  • If you want the Session to execute the update() statement only when the properties of the News object are modified, you can set the select-before-update of the elements in the mapping file to true. The default value of this property is false

  • When the update() method is associated with a free object, if a persistent object with the same OID already exists in the Session cache, an exception will be thrown
  • When the update() method is associated with a detached object, an exception will also be thrown if the corresponding record does not exist in the database.

Session's merge() method

write picture description here

Session's delete() method

The delete() method of Session can delete a free object or a persistent object.

Session's delete() method processing process: Plan to execute a delete statement to delete the object from the Session cache, and the object enters the delete state.

There is a hibernate.use_identifier_rollback property in Hibernate's cfg.xml configuration file, its default value is false, if it is set to true, it will change the operation behavior of the delete() method: The delete() method will make persistent objects or free objects The OIDs are set to null, making them temporary objects

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325769601&siteId=291194637