In this post, we will discuss important Session interface methods to perform database operations in Java Hibernate applications.
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate TutorialA Session is used to get a physical connection with a database. The Session object is lightweight and designed to be instantiated each time an interaction is needed with the database. Persistent objects are saved and retrieved through a Session object.
Overview of Session Interface
The Session interface is the main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service.
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)
The main methods of the Session interface are to create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:
There are a number of methods provided by the Session interface, but I'm going to list down a few important methods that have been frequently used:
The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)
The main methods of the Session interface are to create, read and delete operations for instances of mapped entity classes. Instances may exist in one of three states:
- transient − A new instance of a persistent class, which is not associated with a Session and has no representation in the database, and no identifier value is considered transient by Hibernate.
- persistent − You can make a transient instance persistent by associating it with a Session. A persistent instance has a representation in the database, an identifier value, and is associated with a Session.
- detached − Once we close the Hibernate Session, the persistent instance will become a detached instance.
Session Interface Methods
- Transaction beginTransaction() - Begin a unit of work and return the associated Transaction object.
- void cancelQuery() - Cancel the execution of the current query.
- void clear() - Completely clear the session.
- Connection close() - End the session by releasing the JDBC connection and cleaning up.
- Criteria createCriteria(Class persistentClass) - Create a new Criteria instance, for the given entity class, or a superclass of an entity class.
- Criteria createCriteria(String entityName) - Create a new Criteria instance, for the given entity name.
- Serializable getIdentifier(Object object) - Return the identifier value of the given entity as associated with this session.
- Query createFilter(Object collection, String queryString) - Create a new instance of Query for the given collection and filter string.
- Query createQuery(String queryString) - Create a new instance of Query for the given HQL query string.
- SQLQuery createSQLQuery(String queryString) - Create a new instance of SQLQuery for the given SQL query string.
- void delete(Object object) - Remove a persistent instance from the datastore.
- void delete(String entityName, Object object) - Remove a persistent instance from the datastore.
- Session get(String entityName, Serializable id) - Return the persistent instance of the given named entity with the given identifier, or null if there is no such persistent instance.
- SessionFactory getSessionFactory() - Get the session factory which created this session.
- void refresh(Object object) - Re-read the state of the given instance from the underlying database.
- Transaction getTransaction() - Get the Transaction instance associated with this session.
- boolean isConnected() - Check if the session is currently connected.
- boolean isDirty() - Does this session contain any changes which must be synchronized with the database?
- boolean isOpen() - Check if the session is still open.
- Serializable save(Object object) - Persist the given transient instance, first assigning a generated identifier.
- void saveOrUpdate(Object object) - Either save(Object) or update(Object) the given instance.
- void update(Object object) - Update the persistent instance with the identifier of the given detached instance.
- void update(String entityName, Object object) - Update the persistent instance with the identifier of the given detached instance.
You can check out all Session interface methods at the official Javadoc at https://docs.jboss.org/hibernate/orm/5.2/javadocs/org/hibernate/Session.html
Important Session Interface Methods with Examples
Check out below each article explained important Session interface methods with a complete example:
- Hibernate 5 - Save an Entity Example - In this article, we will create a simple Hibernate application to demonstrate how to save an entity into a database.
- Hibernate 5 - Persist an Entity Example - In this article, we will create a simple Hibernate application to demonstrate how to persist an entity into a database.
- Hibernate 5 - saveOrUpdate() Method Example - In this article, we will create a simple Hibernate application to demonstrate how to save or update an entity in the database using the saveOrUpdate() method.
- Hibernate 5 - get(), load() and byId() Method Examples - In this article, we will show you how to use Session.get(), Session.load() and Session.byId() methods to retrieve an entity from database.
- Hibernate 5 - merge() Example - In this article, we will show you how to use Session.merge() method to merge an entity in Hibernate Application.
- Hibernate 5 - Delete or Remove an Entity Example - In Hibernate, an entity can be removed from a database by calling the Session.delete() or Session.remove(). Using these methods, we can remove a transient or persistent object from the datastore.
- Hibernate 5 - load() Method Example - In this article, we will create a simple Hibernate application using Java configuration without using hibernate.cfg.xml to demonstrates the usage of Session.load() method.
- Hibernate 5 c3p0 Connection Pool Example - In this article, we will show how to use c3p0 connection pooling in hibernate applications.
Key Points
- A Session instance is serializable if its persistent classes are serializable.
- If the Session throws an exception, the transaction must be rolled back and the session discarded. The internal state of the Session might not be consistent with the database after the exception occurs.
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate Tutorial
References
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment