Java Hibernate Objects
Since I have recently learned Java Hibernate, I have compiled the information on several states of Java Hibernate objects here. Friends who are interested can take a look.
Transient: No data in the database corresponds to it. If it exceeds the scope, it will be collected by the JVM garbage collector. It is usually an object that comes out of new and has no association with the session.
Persistent: There is data in the database corresponding to it, and it is currently associated with the session, and the associated session is not closed and the transaction is not committed;
The state of a persistent object changes, which will affect the database when the transaction is committed (hibernate can detect).
(When the object is saved, the data will be persisted. At this time, after obj.set(), the transaction will be submitted to hibernate.
If you use obj.set() before saving the object (even if it is in a transaction), that is, before persisting the object, then hibernate cannot detect data modification and will not affect the database)
Detached: There is data in the database corresponding to it, but there is currently no session associated with it; the status of the detached object changes, and hibernate cannot be detected.
Three states of the hibernate object, transformation diagram:
Commit() will close the session
Interview: What if you determine what state an object is in?
The main basis is: 1. Check whether the object is in session, 2, and see if there are corresponding records in the database.
Summarize:
Transient state: There is no session management, and the database has no corresponding records
Persistence: There is session management and records in the database
De-tube/free state: There is no session management, but it is recorded in the database.
Thank you for reading, I hope it can help you. Thank you for your support for this site!