Method of Session Interface in Hibernate
1. isOpen(): This method determines whether the session is open or closed and returns a boolean value.
2. isDirty(): This method checks to see if the session has changed and returns a boolean value.
3. isConnected(): This method evaluates if the session is connected or not and returns boolean values.
4. save(Object obj): Using this technique, the primary key is generated, and the record is added to the database.
5. update(Object obj): The database record is updated using this technique.
6. saveorUpdate(Object obj): The item given determines whether this function saves or updates.
7. delete(String entity, Object obj): The persistent instance is removed using this procedure.
8. clear(): This technique terminates the session.
9. get(String entity, Serializable id): The persistence status of the specified Entity is returned by this method.
10. refresh(Object obj): The entity is updated with the most recent data using this technique.
11. cancelQuery(): This technique stops the current query from running.
12. createQuery(String SQL): Query is created using this method as a new instance.
Hibernate session creation example
If the persistent classes in a session may be serialized, the session instance is serializable. The typical transaction should use the following idiom:-
Session sessions = factory.openSession();
Transaction transaction = null;
try
{
transaction = sessions.beginTransaction();
// write your code here
...
transaction.commit();
}
catch (Exception e)
{
if (transaction !=null)
{
transaction.rollback();
}
e.printStackTrace();
}
finally
{
sessions.close();
}
The transaction must roll back and delete the session if it throws an exception.
Advantages of Hibernate Session
- Hibernate sessions adhere to the database's ACID (Atomicity, Consistency, Isolation, and Durability) properties.
- It is database-independent; thus, it is used with any database, including MySQL and Oracle.
- Even rudimentary comprehension of SQL should be enough to help you comprehend how it functions.
- Because of its uniform object mapping, the code has fewer potential flaws and faults.
- Hibernate provides hierarchical caching, which increases the effectiveness of the coding process.
Frequently Asked Questions
Is the SessionFactory object thread-safe?
Yes, SessionFactory is a thread-safe object, meaning multiple threads cannot access it simultaneously.
What is Session?
It keeps the database and the Hibernate application connected. It offers factory methods that return these instances because it is a factory for Query, Criteria, and Transaction.
Is a session object thread-safe?
Sessions are not thread-safe objects; several threads may access them at once. To put it another way, you can distribute it among threads.
Define persistent class.
Persistent classes are those whose objects are stored in a database table.
Conclusion
Congrats on finishing the blog! This blog went into great detail about hibernate sessions, including its approach, benefits, and creation.
If you want to explore more blogs on this topic, please follow these blogs especially curated for readers like you - JDBC, JDBC Connection, Guide to ORM, Spring Boot JDBC.
Please refer to our guided pathways on Code studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.
Please do upvote our blogs if you find them helpful and informative!
Happy learning!