Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Hibernate session


May 17, 2021 Hibernate


Table of contents


Session

Session is used to obtain a physical connection to the database. S ession objects are lightweight and are designed to be instantiated each time you need to interact with the database. Persistent objects are saved and retrieved through the Session object.

Session objects should not remain open for long periods of time because they are generally not thread-safe and should be created and destroyed as needed. S ession's primary function is to provide creation, read, and delete operations for mapping instances of entity classes. These instances may exist in one of three states at a given point in time:

  • Instant State: A new persistent instance, considered transient by Hibernate, is not associated with Session, has no records associated with it in the database, and has no identifier values.
  • Persistence: An instantaneous instance can be transformed into a persistent state instance by association to a Session. Persistent state instances do not have records associated with them in the database, have identifier values, and are associated with a Session.
  • De-piped state: Once Hibernate Session is closed, the persistent state instance becomes the de-piped instance.

If the persistent state category of a Session instance is serialized, the Session instance is serialized. A typical transaction should use the following syntax:

Session session = factory.openSession();
Transaction tx = null;
try {
   tx = session.beginTransaction();
   // do some work
   ...
   tx.commit();
}
catch (Exception e) {
   if (tx!=null) tx.rollback();
   e.printStackTrace(); 
}finally {
   session.close();
}

If Session throws an exception, the transaction must be rolled back and the session must be discarded.

Session interface method

The Session interface provides a number of methods, but in the following instructions I'll list only a few important methods that we'll apply in this tutorial. You can view the Hibernate file and query the full method catalog related to Session and SessionFactory.

Serial number Session methods and instructions
1 Transaction beginTransaction()
Start the work unit and return the associated transaction object.
2 void cancelQuery()
Cancel the current query execution.
3 void clear()
Clear the session completely.
4 Connection close()
End the session by releasing and cleaning up the JDBC connection.
5 Criteria createCriteria(Class persistentClass)
Create a new Criteria instance for a given entity class or supernat of an entity class.
6 Criteria createCriteria(String entityName)
Create a new Criteria instance for the given entity name.
7 Serializable getIdentifier(Object object)
Returns the identifier value of the session associated with a given entity.
8 Query createFilter(Object collection, String queryString)
Create a new instance of the query for a given collection and filter characters.
9 Query createQuery(String queryString)
Create a new instance of the query for a given HQL query character.
10 SQLQuery createSQLQuery(String queryString)
Create a new instance of SQLQuery for a given SQL query string.
11 void delete(Object object)
Remove persistent instances from the data store.
12 void delete(String entityName, Object object)
Remove persistent instances from the data store.
13 Session get(String entityName, Serializable id)
Returns a persistent instance of a given name with a given identifier or null, if there is no such persistence instance.
14 SessionFactory getSessionFactory()
Gets the session factory that created the session.
15 void refresh(Object object)
Reread the state of a given instance from the base database.
16 Transaction getTransaction()
Gets the transaction instance associated with the session.
17 boolean isConnected()
Check if the current session is connected.
18 boolean isDirty()
Does the session contain changes that must be synchronized with the database?
19 boolean isOpen()
Check that the session is still on.
20 Serializable save(Object object)
A generated identity is assigned first to maintain a given instantaneous instance.
21 void saveOrUpdate(Object object)
Save (object) or update (object) a given instance.
22 void update(Object object)
Updates persistent instances with identifiers that are given instances that are out of control.
23 void update(String entityName, Object object)
Updates persistent instances with identifiers that are given instances that are out of control.