Detailed explanation of Java hibernate objects (transient state, persistent state, off pipe state)

Java hibernate object

Due to the recent learning of Java hibernate, several states of Java hibernate objects are sorted out here. Interested friends can have a look.

Transient: there is no data corresponding to it in the database. If it exceeds the scope, it will be recycled by the JVM garbage collector. Generally, it is an object that is new and has no association with the session.

Persistent: there is data corresponding to it in the database. It is currently associated with a session, and the associated session is not closed and the transaction is not committed;

The state of the persistent object changes, which will affect the database when the transaction is committed (hibernate can detect it).

(after the object is saved, the data will be persisted. In this case, the transaction will be committed after obj. Set(). Hibernate will detect the modification of the data.

If the object is saved (even in a transaction), that is, before the object is persisted, use obj Set(), hibernate cannot detect data modification and will not affect the database)

Detached: there is data corresponding to it in the database, but there is no session associated with it at present; The state of the unmanaged object changes and Hibernate cannot detect it.

Three states of Hibernate objects, transition diagram:

Commit() closes the session

Interview chart: how to judge the status of an object?

The main basis is: 1 Check whether the object is in session. 2. Check whether there are corresponding records in the database

Summary:

Transient: there is no session management, and there is no corresponding record in the database

Persistent state: it has session management and records in the database

Off pipe state / free state: there is no session management, but there are records in the database

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>