Different states of an entity bean
Explanation
In Hibernate, an entity bean can exist in one of three states: transient, persistent, or detached. These states define the lifecycle and behavior of the entity within the context of a Hibernate session.
1. Â Transient State :
- Definition : An entity is in the transient state if it is just created using the new operator and is not associated with any Hibernate session
Characteristics
- The entity is not stored in the database.
- Hibernate is not aware of the entity’s existence.
- No identifier (primary key) value is assigned unless explicitly set.
2. Â Persistent State
- Definition : An entity is in the persistent state if it is associated with a Hibernate session and is being tracked by Hibernate.
- Characteristics :
- Â The entity is stored in the database.
- Â Any changes made to the entity are automatically synchronized with the database when the session is flushed.
- The entity is assigned an identifier (primary key) by Hibernate.
3. Â Detached State
- Definition : An entity is in the detached state if it was previously in the persistent state but the associated session has been closed or the entity has been evicted from the session.
- Characteristics :
- The entity is no longer tracked by Hibernate.
- Changes made to the entity will not be automatically synchronized with the database unless it is re-attached to a new session. Â Â Â Â –
- The entity retains its identifier (primary key
Table of Contents
Example
Example in Java:
java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class EntityStateExample {
public static void main(String[] args) {
// Create SessionFactory
SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
// Transient state
Student transientStudent = new Student("John", "Doe", "john.doe@example.com");
Session session = sessionFactory.openSession();
try {
// Start transaction
session.beginTransaction();
// Persistent state
session.save(transientStudent);
System.out.println("Persisted: " + transientStudent);
// Commit transaction
session.getTransaction().commit();
} finally {
session.close();
}
// Detached state
System.out.println("Detached: " + transientStudent);
session = sessionFactory.openSession();
try {
// Start transaction
session.beginTransaction();
// Re-attach and update the entity
transientStudent.setFirstName("Jane");
session.update(transientStudent);
System.out.println("Updated: " + transientStudent);
// Commit transaction
session.getTransaction().commit();
} finally {
session.close();
sessionFactory.close();
}
}
}
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "email")
private String email;
// Constructors
public Student() {}
public Student(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
// Getters and setters
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
}
}
Example of Different states of an entity bean
- The Student object transientStudent starts in the transient state.
- After being saved in the session, it moves to the persistent state.
- When the session is closed, it moves to the detached state.
- The detached entity is then re-attached and updated in a new session.