Hibernate persisting the state of an object

Source: Internet
Author: User

Standing in the perspective of persistence, Hibernate divides objects into 4 states: persistent state, temporary state, Free State, delete state. The specific method of the Session allows the object to transition from one state to another.

persisting the state of an objecttemporary Object (Transient):
– OID is usually null in the case of a proxy primary key-not in Session Cache – There are no corresponding records in the database
persisted objects (also called "managed") (Persist):
– oid not null – session cache – If there is already a record in the database and its corresponding records, persisted objects and related records in the database correspond to –session Span style= "Color:blue" in flush cache – session Span style= "color:red" in the cache of the instance Each record in a database table only corresponds to a unique persisted object

Delete Object (removed)

– No records in the database that correspond to its OID – no longer in session cache – in general, applications should not use deleted objects

Free Object (also known as "off-pipe") (Detached):
OID is not null
is no longer in the Session cache
As a general rule, a free object is transformed by a persistent object, so there may be records in the database that correspond to it.


state transition diagram for an object

Save () and persist ()
  • The Save () method of the Session turns a temporary object into a persisted object
  • The Save () method of the Session completes the following actions:
    – Add the News object to the Session cache to get it into a persistent state – Use the identifier generator specified by the mapping file to assign a unique OID to the persisted object . The SetId () method setting the OID for the news object is not valid in the case of a proxy primary key. The database is automatically reassigned ID – plan to execute an insert statement: Flush when the cache
  • Hibernate maintains its correspondence with database-related records by persisting the OID of the object. When the News object is in the persisted state, do not allow the program to modify its ID arbitrarily
  • The persist () and save () differences (persist () also perform insert operations):
– When the Save () method is executed on an OID object that is not NULL, the object is saved to the database as a new OID; However, an exception is thrown when the persist () method is executed. (before calling the persist method, if the object already has an ID, the insert is not executed, and an exception is thrown)

get () and load ()
    • Can load a persisted object from the database according to the OID that was set.
    • Executes the load method, and if the object is not used, the query operation is not performed immediately and a proxy object is returned
    • Difference:
      – When the database does not have a record corresponding to the OID, and the session is not closed, load () is not a problem if the object is not used, if initialization is required, the objectnotfoundexception exception is thrown, and the Get () method returns null– The two use different deferred retrieval strategies:Theload method supports deferred load policies. and get is not supported. –Load () may throw a Lazyinitializationexception exception: The session has been closed before the proxy object needs to be initialized


Update ()
    • session The Update () method to turn a free object into a persisted object and plan to execute a UPDATE statement.
    • Note:
1. The UPDATE statement is sent regardless of whether the free object to be updated is consistent with the database record How can you let the Update method not blindly trigger the UPDATE statement? If you want the Session to execute the update () statement only if you modify the properties of the News object, you can set the select-before-update of the <class> element in the mapping file to True. The default value for this property is False, but it is generally not necessary to set this property (a single SELECT statement is required in case of update) 2. When the update () method associates a free object, an exception is thrown if no corresponding record exists in the database. 3. when the update () method associates a free object, an exception is thrown if a persisted object of the same OID already exists in the cache of the Session. because you cannot have two OID objects in the session cache.
saveorupdate ()

    • The Saveorupdate () method of the Session also contains the function of the Save () and update () methods

    • Criteria for determining objects as temporary objects
      – The OID of theJava object is null– The Unsaved-value property is set for <id> in the mapping file, And the OID value of the Java object matches this Unsaved-value property value
    • Attention

1. If the OID is not NULL, but the data table does not have a corresponding record, an exception of 2 is thrown. Understanding: An object that has an OID value equal to the ID of the Unsaved-value property is also considered a free object
merge ()


Delete ()
    • Performs a delete operation, which is prepared to perform a delete operation as long as the OID is corresponding to a record in the data table, and throws an exception if the OID does not have a corresponding record in the datasheet
    • The delete () method of the Session can either delete a free object or delete a persisted object
    • Session Delete () method processing – plan to execute a DELETE statement (whether or not the object has an OID before committing it) – after committing, the object is removed from the session cache and the object goes into the delete state.
    • Hibernate has a hibernate.use_identifier_rollback property in the Cfg.xml configuration file with a default value of false, which, if set to true, alters the run behavior of the Delete () method: The Delete () method sets the OID of the persisted object or the free object to null, making them a temporary object
evict ()
    • Removes the specified persisted object from the session cache

calling stored procedures through HibernateWork Interface: Access to the database directly through the JDBC API

the DoWork (work) method of the Session is used to performThe action specified by the work object, that is, the execute () method that invokes the working object. The Session passes the currently used database connection to the Execute () method.


Hibernate and triggers work togetherHibernate can cause two types of problems when working with triggers in the database
– The trigger causes the persisted object in the session's cache to be inconsistent with the corresponding data in the database: the trigger runs in the database, and the action it performs is blindly firing the trigger on the –session update () method, which is transparent to the session: The UPDATE statement is executed regardless of whether the property of the free object has changed, and the UPDATE statement fires the corresponding trigger in the database
Solution:
– immediately after the session has been performed, the Flush () and Refresh () methods of the session are called to force the session's cache to synchronize with the database (the Refresh () method reloads the object from the database)
-Set the Select-before-update property in the <class> element of the mapping file: When the Session's update or Saveorupdate () method updates a Free object, the SELECT statement is executed to obtain the Up-to-date data from the previous free object in the database, which executes the UPDATE statement only if it is inconsistent



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.