Hibernate (three statuses of hibernate objects)

Source: Internet
Author: User

Transient: the database does not have the corresponding data. If it exceeds the scope, it will be reclaimed by the JVM garbage collector. Generally, it is a new object that is not associated with the session. Persistent: the database may have the corresponding data (save). Currently, the database is associated with the session. The associated session is not closed, and the transaction is not committed. The persistent Object status changes, the database is affected when the transaction is committed (which can be detected by hibernate ). Detached: the database may have data that corresponds to it, but there is no session associated with it, but there is an oid. The status of the hosted object is changed and cannot be detected by hibernate. How to determine whether it is related to the session and whether the database records the instantaneous state of switching from three States: a java object has just been created through the new statement and is in the temporary state, at this time, the save () method of the persistent state session is not matched with any records in the database. The instantaneous object --> is converted into the load () method and get () method of the Persistent Object session, the returned object is always in the persistent state session update (), saveOrUpdate (), lock (), free object --> Persistent Object. When a persistent object is associated with a temporary object, when cascade storage is allowed, the session changes the temporary object to the Free State of the Persistent object when the session close () method is called, the session cache is cleared, and all persistent objects in the cache become free objects. If no variables are referenced in the application to reference these free objects, they will end the evict () of the session lifecycle () the method can delete a persistent object from the cache and change it to a free state. State Image Description three relations save: instantaneous state --> persistent State update: Free State --> persistent State saveOrRpdate: unable to determine whether it is instantaneous or free. You can call this method to change it to permanent state, the system automatically determines whether the object is in the instantaneous or free state. The database may have no information, but the oid still exists. The deletion State may show how to determine the object state and manipulate the persistence object save () the save () method of Session converts a temporary object to the save () method of the Persistent Object Session to complete the following operations: add the News object to the Session cache, to enable persistence, select the identifier generator specified by the ing file and assign a unique OID to the persistence object. when the proxy primary key is used, the setId () method sets the OID to invalidate for the News object. (usually automatically generated) Hibernate maintains its correspondence with database-related records through the persistence object OID. when the News object is in the persistent state, The program is not allowed to arbitrarily modify its ID (how to insert the corresponding relationship in the table) and execute an insert statement to assemble the current attribute value of the Customer object into the insert statement. The SQL insert statement is executed only when the session clears the cache. If the attribute value of the persistence object is modified after the save () method, when the session clears the cache, the SQL update statement (for example, below) is executed to add the code for modifying the name under 23 rows, determine what results are actually inserted into the database as follows [principle] The new object becomes persistent, and the user is associated with the session. hibernate knows any changes to the user, the saved user has not been inserted into the database. The updated () method of the abc update () Session is saved in the table to convert a free object to a persistent object, and execute an update statement. saveOrupdate () This method includes both the save and update Methods. If the parameter is a temporary object, the save method is used. If the parameter is a free object, the update method is used. If the parameter is a persistent object, the system returns the result directly.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.