Hibernate Framework Architecture and Basics
Hibernate Hello World Tutorial
Hibernate XML Configuration Example
Hibernate Java Configuration Example
Hibernate Transaction Management
Hibernate/JPA - Primary Key Generation Strategies
JPA and Hibernate Cascade Types
Hibernate - Save an Entity Example
Hibernate - Persist an Entity Example
Hibernate - saveOrUpdate() Method Example
Hibernate - get(), load() and byId() Method
Hibernate - merge() Example
Hibernate - Delete or Remove an Entity Example
Hibernate - load() Method Example
Hibernate Session.clear() Method Example
Hibernate One to One Mapping
Hibernate One to Many Mapping
Hibernate Many to Many Annotation
Hibernate One to Many CRUD Example
Hibernate One to One CRUD Example
Hibernate Inheritance Mapping
Hibernate Query Language
Hibernate CRUD Operations Example
Hibernate Session Interface Methods
JSP Servlet Hibernate CRUD Example
Hibernate Registration Form Example
Login Form using JSP + Servlet + Hibernate + MySQL
More .........
In this article, we will discuss what are Hibernate object states with examples.
Hibernate Object States
Hibernate defines and supports the following object states:
- Transient
- Persistent
- Detached
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate Tutorial
Transient
An object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session.For example, here we are creating a Student object using a new operator, and note that we are not saving a student object to a database using Hibernate Session.
Student s1 = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
Student s2 = new Student("John", "Cena", "johncena@javaguides.com");
Newly instantiated instances of a persistent class are considered transient by Hibernate.
We can make a transient instance persistent by associating it with a session:
Student student = new Student("Ramesh", "Fadatare", "rameshfadatare@javaguides.com");
Student student1 = new Student("John", "Cena", "john@javaguides.com");
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction = session.beginTransaction();
// save the student objects
session.save(student);
session.save(student1);
// commit transaction
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
e.printStackTrace();
}
Persistent
A persistent instance has a representation in the database and an identifier value. It might just have been saved or loaded, however, it is by definition in the scope of a Session.Hibernate will detect any changes made to an object in a persistent state and synchronize the state with the database when the unit of work completes. Developers do not execute manual UPDATE statements or DELETE statements when an object should be made transient.
Newly instantiated instances of a persistent class are considered transient by Hibernate. We can make a transient instance persistent by associating it with a session.
Below diagram shows the snippet of saving an entity in a database:
If we want to save an object into a database then we need to call any one of the following 3 methods:
- save()
- persist()
- saveOrUpdate()
Detached
A detached instance is an object that has been persistent, but its Session has been closed. The reference to the object is still valid, of course, and the detached instance might even be modified in this state.
A detached instance can be reattached to a new Session at a later point in time, making it (The update() and merge() methods are used to reattach the detached objects to a session) persistent again.
We can detach an object from the session by using the three methods.
session.clear();
session.evict(student);
session.close();
Learn complete JPA at JPA Tutorial - Java Persistence API
Learn Hibernate ORM Framework at Hibernate Tutorial
Comments
Post a Comment
Leave Comment