Statement
Session is a persistence manager. Before performing operations on the database, you must first obtain the session instance. This is equivalent to JDBC. Before performing operations on the database, you must obtain the connection instance. session is the basis of hibernate operations, it is not thread-safe, but we can use threadlocal (used to store copies of variables of every thread. The keys of elements in map are thread objects, and the values correspond to copies of variables of the thread, in this way, each thread can change its own copy independently without conflict with the copies of other threads .) To ensure thread security.
Status description
1. Transient object ):
Objects initialized using the new operator cannot be immediately persistent. Their status is instantaneous, that is, they do not have any behavior associated with the database, there is no matching record in the database, and there is no session management.
Summary:
1. Not managed by session
2. No matching records exist in the database.
2. Persistence object ):
A persistent instance is any instance with a database ID. It has a persistence Manager session for unified management. Persistent instances are operated in transactions. Their State transactions are synchronized with the database at the end of the transaction. When a transaction is committed, the status in the memory is synchronized to the database by executing the SQL insert, update, and delete statements.
Summary:
1. Include in session management
2. There are matching records in the database
3. When the attribute changes, the cache is cleared (dirty data check) and will be automatically synchronized with the database
3. Detached object ):
After the session is closed, the persistent object becomes an offline object. Offline means that this object cannot be synchronized with the database, and they cannot be managed by hibernate.
Summary:
1. Not managed by session
2. matched records exist in the data.
Legend Annotation
Sometimes the text is pale. How to better display our own thinking requires us to use the power of graphics, so our thinking is more profound. Please refer to the three state diagrams of session, let you know how the status is converted.
(Change the session status through a series of methods)
Three States are distinguished:
1. There is no ID (if not, it is in transient state)
2. Is the ID in the database?
3. Is there a session cache in the memory)
Summary
We must have a certain understanding of the session status, so we will summarize the differences to facilitate our understanding.
Transient object: It may be recycled by the garbage collector at any time (the corresponding record is not found in the database and should be new initialization). After the SAVE () method is executed, the persistent object is not included in session management. (An object in memory, neither ID nor cache)
Persistent Object: a corresponding record exists in the database and is included in session management. When the cache is cleared (dirty data check), it is synchronized with the database. (Memory, cache, and database (ID)
Detached object: it may also be recycled by the garbage collector (there are corresponding records in the database, but no object is referenced. It refers to session reference). The injection state passes through the persistent state, not included in session management. (Memory, cache, and database)