ChatGPT解决这个技术问题 Extra ChatGPT

What are detached, persistent and transient objects in hibernate?

What are detached, persistent and transient objects in hibernate? Please explain with an example.

upon session.close won't it remove all objects those were persistance in the session scope?
Yes it will remove so whenever you close a session persistent state of an object is converted to Detached state.
@Rachel nice link provided with code for deeper understanding
Have a look at youtube.com/…

F
FranciscoPedreira

A new instance of a persistent class which is not associated with a Session, has no representation in the database and no identifier value is considered transient by Hibernate:

Person person = new Person();
person.setName("Foobar");
// person is in a transient state

A persistent instance has a representation in the database, an identifier value and is associated with a Session. You can make a transient instance persistent by associating it with a Session:

Long id = (Long) session.save(person);
// person is now in a persistent state

Now, if we close the Hibernate Session, the persistent instance will become a detached instance: it isn't attached to a Session anymore (but can still be modified and reattached to a new Session later though).

All this is clearly explained in the whole Chapter 10. Working with objects of the Hibernate documentation that I'm only paraphrasing above. Definitely, a must-read.


An identifier value is probably not a strict condition for an object to be persistent since I could use assigned identifier generation strategy.
Besides closing the session that read in the instance, one can detach an instance by calling session.evict(). This removes it from being managed by Hibernate (thus prevent changes to the instance from being automatically propagated back to the db). You can send changes to the db via session.update(), and re-attach it with session.merge().
We many time dont follow original docs provided by frameworks.. which sometime itself have hidden information with proper description just need to explore. only prob is we don't find it easily :)
A persistent instance has a representation in the database A persistent entity hasn't a representation in the database before committing the save operation.
So, detached and transient entity difference is presence of ID? If I create new instance with assigned id, then formally it is transient, because it is not persisted yet, but can't be distinguishable from detached, because it has an ID. Right?
S
Sk Sharma

Object in hibernate has following states:

Transient - Objects instantiated using the new operator are called transient objects.

An object is transient if it has just been instantiated using the new operator, and it is not associated with a Hibernate Session. It has no persistent representation in the database and no identifier value has been assigned. Transient instances will be destroyed by the garbage collector if the application does not hold a reference anymore.

Persistent - An object that has a database identity associated with it is called a persistent object.

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 persistent state and synchronize the state with the database when the unit of work completes.

Detached - A detached instance is an object that has been persistent, but its Session has been closed.

A detached instance can be reattached to a new Session at a later point in time, making it persistent again. This feature enables a programming model for long running units of work that require user think-time. We call them application transactions, i.e., a unit of work from the point of view of the user.

http://webiwip.com/interview-questions-answers/hibernate-interview-questions/32012


S
Sabunkar Tejas Sahailesh

Let me explain in Garbage collector point of view also.

There are 3 Object states of hibernate (or) Object Scope of hibernate-

Transient state persistent state detached state

It is better to understand with a code example-

Let us consider a POJO class as Student Object->

Student student = new Student(); 

Now, this student object is at transient state.

When we attache this POJO object to hibernate session->

session.save(student);

Now this POJO object is at persistent state.

(Garbage collector point of view- GC cannot wipe-out Any object which is in the persistent state. Soo we can say that persistent state is like temporary storage for POJO objects)

If we perform->

session.beginTransaction.commit();

then the POJO object is at Permanent or Database storage state

(Garbage collector point of view- GC cannot wipe-out this object because this POJO object is now outside the scope of JVM and stored in the form table inside a database.Soo we can say that this Database storage state is like permanent storage for POJO objects)

If we perform->

session.evict(student); 

then POJO object is evicted or removed back from the persistent state to detached state.Soo this state of POJO object is detached state.

(Garbage collector point of view- GC can easily wipe-out the detached state POJO object from JVM)


O
O.Badr

Given the following entity:

@Entity
public class City {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)    
    private long id;

    // other fields and methods.
}

From Hibernate 5.2 documentation (I've also included the removed state):

transient the entity has just been instantiated and is not associated with a persistence context. It has no persistent representation in the database and typically no identifier value has been assigned (unless the assigned generator was used).

City city = new City();

managed, or persistent the entity has an associated identifier and is associated with a persistence context. It may or may not physically exist in the database yet.

// city will be in a managed/persistent state and any changes to it, will be tracked by hibernate
// and reflected to the database when the persistence context is flushed.
session.save(city);

detached the entity has an associated identifier, but is no longer associated with a persistence context (usually because the persistence context was closed or the instance was evicted from the context)

// city is in a detached state, Hibernate is no longer aware of the entity 
session.evict(city)

removed the entity has an associated identifier and is associated with a persistence context, however it is scheduled for removal from the database.

session.remove(city);


Note: Hibernate API offers couples of methods to switch between entity states, and I think it's worth exploring a Hibernate Session class.


A
Amol Dixit

Beside the correct answer already identified persistent, transient, detached are just the state of the object in hibernate.

To be more precise, these three states actually show the hibernate object changes and the session life cycle status