Siege Lion on the Way (one) hibernate (vi)---manipulating objects through hibernate (top)

Source: Internet
Author: User


First,Hibernate cache Introduction :
The session interface is the primary interface that hibernate provides to the application to manipulate the data interface, which provides basic methods for saving, updating, deleting, and loading Java objects.
The session has a cache, the object in the cache is called a persisted object, and the session is able to update the database synchronously at some point in time, which is the process of clearing the cache.
Hibernate divides objects into 4 states: persistent state, temporary state, Free State, and delete state.

Second,the session cache :
The session's cache is composed of a series of Java collections.
1,the role of Session cache :
A, reduce the frequency of access to the database.
B, when there is a cyclic correlation between persisted objects in the cache, the session guarantees that there will be no dead loops to access the object graph, and the JVM stack overflow exception caused by the dead loop.
C, ensure that the relevant records in the database and the corresponding objects in the cache are kept synchronized. The session is synchronized by dirty checking when the cache is cleaned.
2, dirty Check and the mechanism to clean up the cache :
When an object is added to the session cache, the session copies a snapshot of the properties of the object's value type. When cleaning up the cache for dirty checking, the object's current property is compared to its snapshot to determine if a change has occurred.
In addition, when the object property changes, the session does not immediately clean up the cache and executes the associated SQL operations, but cleans up the cache at a specific time. The advantage is that the session can combine several related SQL statements into one SQL, reducing the number of accesses to the database.

  when the session cleans up the cache, execute the SQL statement in the following order (understanding):

?
1 2 3 4 5 6 A、按照session.save()方法先后顺序,执行所有对实体进行插入的insert语句。 B、执行所有对实体进行更新的update语句。 C、执行所有对集合进行删除的delete语句。 D、执行所有对集合元素进行删除、更新或者插入的SQL语句。 E、执行所有对集合进行插入的insert语句。F、按照应用程序调用session.delete()方法的先后顺序,执行所有对实体进行删除的delete语句。

  the session cleans up the cache at the following point in time :

?
1 2 3 A、当应用程序调用org.hibernate.Transaction分commit()方法时,先清理缓存,再提交事务。 B、当应用程序执行一些查询操作时,如果缓存中的持久化对象属性发生了变化,就会先清理缓存。C、当应用程序显式调用Session的flush()方法的时候。

Exception: If the object uses the native generator to generate oid,name, the INSERT statement is executed immediately when the object is saved by the Save () method that invokes the session.

You can also explicitly set the point in time to clean up the cache by using the Session.setflushmode () method.

In most cases, the application does not need to explicitly call the flush () method, except for the following common exceptions:
A, inserting, deleting, or updating a persisted object raises the trigger in the database.
B, when mixing hibernate APIs with the JDBC API in your application.
C, the JDBC driver is not robust, causing hibernate to not work properly in auto-clean cache mode.

Third, thestate of the Java object in Hibernate persistence layer :
1. Status Description :
A, temporary State (transient): Just created with the new statement, has not been persisted, and is not in the session's cache.
B, persistent object (persistent): Has been persisted and added to the session cache.
C, delete state (delete): It is no longer in the session's cache, and the session is scheduled to delete it from the database.
D, Free State (detached): Has been persisted, but is no longer in the session cache.
2. state transition Diagram :

  
3, the characteristics of temporary objects :
A, in the case of a proxy primary key, the OID is usually null.
B, not in the session cache, is not associated with any one session instance.
C, there is no corresponding record in the database.

Iv. Characteristics of Persistent objects :
1, OID is not null.
2, in the cache of a session instance. Persisted objects are always associated with a session instance.
3. Persistent objects correspond to related records in the database.
4. When the session cleans up the cache, the database will be updated synchronously according to the property changes of the persisted object.

V. characteristics of deleted objects :
1, OID is not null.
2. Delete from the cache of a session instance.
3. The deleted object corresponds to the related record in the database.
4. Session has been scheduled to delete it from the database.
5. When the session cleans up the cache, it executes the SQL DELETE statement to delete the corresponding record in the database.
6. In general, applications should no longer use deleted objects.

Vi. Characteristics of free objects :
1, OID is not null.
2. It is no longer in the session cache.
3. The free object is transformed by a persistent object, so there may be records in the database that correspond to it.

Free objects are the same as temporary objects: they are not associated with the session, so they do not guarantee that their property changes are synchronized with the database save.
The difference between a free object and a temporary object is that the free object is transformed by a persisted object, so there may be a corresponding record in the database, and the temporary object does not have a corresponding record in the database.
The free object is the same as the deleted object: Both are not in the session's cache, and there may be corresponding records in the database.
The difference between a free object and the deleted object is that the free object is completely detached from the session, and for the deleted object, the session plans to delete it from the database and delete it when the cache is cleaned up.

Sevendetailed usage of Session interface
1.the Save () and persist () methods of the session
The Save () method turns a temporary object into a persisted object. Note the application should be prevented from modifying the OID.
The persist () method is similar to the Save () method, except that the method is present in the Hibernate3 version Zhongcai. The SQL INSERT statement is not executed if the persist () method is called outside the transaction boundary.
2.the load () and get () methods of the session:
Both methods can load a persisted object from the database based on the given OID.
The difference is:
A the Load () method throws ojbectnotfoundexception when there is no record in the database corresponding to the OID, and the Get () method returns NULL.
B, the two use a different retrieval strategy (retrieval strategy follow-up description). The load () method determines whether lazy loading is based on the lazy property of the <class> element, and the get () method ignores the lazy property and takes an immediate retrieval policy.
Usage scenarios:
A, if the purpose of loading an object is to access its various properties, you can use the Get () method.
B, you can use the load () method if you are loading an object in order to delete it, or to create an association with another object.
3.the update () method of the session
The update () method transforms a free object into a persisted object and plans to execute an UPDATE statement.
If you want the session to execute the UPDATE statement only if the object's properties are modified, you can set the select-before-update of the <class> element to true.
When the update () method associates a free object, an exception is thrown if a persisted object of the same OID already exists in the session cache.
4.the Saveorupdate () method of the session
The Saveorupdate () method contains both the Save () and update () methods. Call the Save () method if the passed parameter is a temporary object, or call the update () method if the passed-in argument is a free object, or return directly if a persisted object is passed in.
Hibernate will consider the object to be temporary when any one of the following conditions is true:
A, the OID of the Java object is null.
B, the Java object has version versioning properties and is null (not understood).
C, the Unsaved-value attribute is set for the <id> element in the mapping file, and the OID of the Java object matches this property value.
D, the Unsaved-value property is set for the version versioning property in the configuration file, and the value of the version versioning property of the Java object matches the property value.
E, provides a custom implementation for Hibernate's interceptor, and Interceptor the Isunsaved () method of the implementation class returns BOOLEAN.TRUE.
5.the Merger () method of the session
The merge () method can copy the properties of a free object into a persisted object.
6.Delete () method for session
The Delete () method is used to delete a Java object from the database. You can delete a persisted object, or you can delete a free object.
7.the Replicate () object of the session
This method can copy objects from one database to another database. (not currently concerned)

Eight, cascade operation object Graph :
1.cascade attribute Table :

( statement: This article is all from "proficient Hibernate:java object Persistence technology detailed" [Sun Weichen Electronics Industry Press] a book. The purpose of this article is only as a study note. If you need to reprint, please indicate the original book related information. )

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.