HIBERNATE4 's session core approach

Source: Internet
Author: User

Tag: Object config False learn to save Exce leave hibernate record

Before learning the core approach of the session, we first understand the status of several objects in hibernate:

Temporary status: This is like a temporary employee of our company, he does not have the relevant information and ID in the company.

Feature: OID is usually null in the case of a proxy primary key
Not in the Session's cache
There are no corresponding records in the database

Persistent state: We are able to understand the official employees of our company.

feature: OID is not null
In the Session cache
If there is already a record in the database and its corresponding records, persist the object and the related record in the database corresponding
Session updates the database synchronously, depending on the properties of the persisted object, when the flush cache
In the cache of the same Session instance, each record in the database table is only the corresponding persisted object

Free State: This is similar to our company's on-the-job leave staff, has its relevant information on the post.

Feature:OID is not null
is no longer in the Session cache
Normally, a free object is transformed by a persistent object, so there may be a corresponding record in the database

Delete Status: This is similar to our employees who have already departed.

Feature: There is no record in the database corresponding to its OID
is no longer in the Session cache
Under normal circumstances, the application should not use the deleted object again

Save method for session

The Save method enables a temporary object to be transformed into a persistent object.

1. Add the object to save to the session. Put it into a persistent state

2. Select the identifier generator specified by the mapping file to assign a unique OID to the persisted object. In the case of a proxy primary key, the SetId () method sets the OID for the News object to invalidate it.
3. Plan to run an INSERT statement: when flush cache
4. Hibernate maintains its corresponding relationship with database related records by persisting the OID of the object. When the News object is persisted, it does not agree that the program arbitrarily changes its ID
5, persist () and save () difference:
When you run the Save () method 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 run.

The Get method and load method of the session

Same point: All can load a persisted object from the database according to the OID
Different points:
The load () method throws a Objectnotfoundexception exception when there is no record in the database corresponding to the OID, and the Get () method returns null
The two use different deferred retrieval strategies: The Load method supports a deferred load policy.

And get is not supported.


Update method for session

1. The update () method of the Session turns a free object into a persisted object and plans to run an UPDATE statement.
2. If you want the Session to run the update () statement only if you change 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 of this property is False
3. When the update () method associates a free object, it is assumed that a persistent object of the same OID already exists in the cache of the Session and throws an exception
4. When the update () method associates a free object, it is assumed that no corresponding record exists in the database, and an exception is thrown.

The Saveorupdate method of the session

1. The Saveorupdate () method of the Session includes the function of the Save () and update () methods at the same time

2, the use of the method of the situation:

Criteria for determining objects as temporary objects
The OID of the Java object is null
The Unsaved-value attribute is set for <id> in the mapping file, and the OID value of the Java object matches the Unsaved-value property value.

Delete method of Session

1. The delete () method of the Session can delete a free object and delete a persisted object.
2, Session of the Delete () method processing process
Plan to run a DELETE statement
Removes the object from the Session cache and enters the delete state.
3, Hibernate cfg.xml configuration file has a Hibernate.use_identifier_rollback property, its default value is False, if it is set to true, will change the execution behavior of the Delete () method: Delete The () method sets the OID of a persisted or free object to null, making them a temporary object


HIBERNATE4 's session core approach

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.