Object States in Hibernate's Session - Baeldung
Object States in Hibernate's Session - Baeldung
(/)
Persistence (https://www.baeldung.com/category/persistence/)
Hibernate (https://www.baeldung.com/tag/hibernate/)
1. Introduction
Hibernate is a convenient framework for managing persistent data, but
understanding how it works internally can be tricky at times.
In this tutorial, we'll learn about object states and how to move between them.
We'll also look at the problems we can encounter with detached entities and
how to solve them.
https://www.baeldung.com/hibernate-session-object-states 1/8
10/3/2021 Object States in Hibernate’s Session | Baeldung
2. Hibernate's Session
The Session
(https://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.
html) interface is the main tool used to communicate with Hibernate. It
provides an API enabling us to create, read, update, and delete persistent
objects. The session has a simple lifecycle. We open it, perform some
operations, and then close it.
When we operate on the objects during the session, they get attached to that
session. The changes we make are detected and saved upon closing. After
closing, Hibernate breaks the connections between the objects and the
session.
3. Object States
In the context of Hibernate's Session, objects can be in one of three possible
states: transient, persistent, or detached.
3.1. Transient
An object we haven't attached to any session is in the transient state. Since
it was never persisted, it doesn't have any representation in the database.
Because no session is aware of it, it won't be saved automatically.
Let's create a user object with the constructor and con rm that it isn't
managed by the session:
3.2. Persistent
https://www.baeldung.com/hibernate-session-object-states 2/8
10/3/2021 Object States in Hibernate’s Session | Baeldung
Alternatively, we may use the save method. The di erence is that the persist
method will just save an object, and the save method will additionally
generate its identi er if that's needed.
3.3. Detached
When we close the session, all objects inside it become detached. Although
they still represent rows in the database, they're no longer managed by any
session:
session.persist(userEntity);
session.close();
assertThat(session.isOpen()).isFalse();
assertThatThrownBy(() -> session.contains(userEntity));
https://www.baeldung.com/hibernate-session-object-states 3/8
10/3/2021 Object States in Hibernate’s Session | Baeldung
Now, we'll create another object with the same identi er as the rst one. This
second object is transient because it's not yet managed by any session, but
we can't make it persistent using the persist method. It's already represented
in the database, so it's not really new in the context of the persistence layer.
Instead, we'll use the merge method to update the database and make the
object persistent:
5. Nested Entities
Things get more complicated when we consider nested entities. Let's say our
user entity will also store information about his manager:
@ManyToOne
private UserEntity manager;
}
https://www.baeldung.com/hibernate-session-object-states 4/8
10/3/2021 Object States in Hibernate’s Session | Baeldung
When we save this entity, we need to think not only about the state of the
entity itself but also about the state of the nested entity. Let's create a
persistent user entity and then set its manager:
assertThatThrownBy(() -> {
session.saveOrUpdate(userEntity);
transaction.commit();
});
java.lang.IllegalStateException:
org.hibernate.TransientPropertyValueException: object references an
unsaved transient instance - save the transient instance before flushing :
com.baeldung.states.UserEntity.manager -> com.baeldung.states.UserEntity
Then, after committing the transaction, we'll be able to retrieve the correctly
saved entity:
transaction.commit();
session.close();
https://www.baeldung.com/hibernate-session-object-states 5/8
10/3/2021 Object States in Hibernate’s Session | Baeldung
@ManyToOne(cascade = CascadeType.PERSIST)
private UserEntity manager;
Now when we persist the object, that operation will be cascaded to all
nested entities:
6. Summary
In this tutorial, we took a closer look at how the Hibernate Session works with
respect to object state. We then inspected some problems it can create and
how to solve them.
As always, the source code is available over on GitHub
(https://github.com/eugenp/tutorials/tree/master/persistence-
modules/spring-boot-persistence-2).
https://www.baeldung.com/hibernate-session-object-states 6/8
10/3/2021 Object States in Hibernate’s Session | Baeldung
CATEGORIES
SPRING (HTTPS://WWW.BAELDUNG.COM/CATEGORY/SPRING/)
REST (HTTPS://WWW.BAELDUNG.COM/CATEGORY/REST/)
JAVA (HTTPS://WWW.BAELDUNG.COM/CATEGORY/JAVA/)
SECURITY (HTTPS://WWW.BAELDUNG.COM/CATEGORY/SECURITY-2/)
PERSISTENCE (HTTPS://WWW.BAELDUNG.COM/CATEGORY/PERSISTENCE/)
JACKSON (HTTPS://WWW.BAELDUNG.COM/CATEGORY/JSON/JACKSON/)
HTTP CLIENT-SIDE (HTTPS://WWW.BAELDUNG.COM/CATEGORY/HTTP/)
SERIES
JAVA “BACK TO BASICS” TUTORIAL (/JAVA-TUTORIAL)
JACKSON JSON TUTORIAL (/JACKSON)
HTTPCLIENT 4 TUTORIAL (/HTTPCLIENT-GUIDE)
REST WITH SPRING TUTORIAL (/REST-WITH-SPRING-SERIES)
SPRING PERSISTENCE TUTORIAL (/PERSISTENCE-WITH-SPRING-SERIES)
SECURITY WITH SPRING (/SECURITY-SPRING)
ABOUT
ABOUT BAELDUNG (/ABOUT)
https://www.baeldung.com/hibernate-session-object-states 7/8
10/3/2021 Object States in Hibernate’s Session | Baeldung
https://www.baeldung.com/hibernate-session-object-states 8/8