Different ways to save the "turn" NHibernate (Save,persist,update,saveorupdte,merge,flush,lock)

Source: Internet
Author: User

Objective

Learn NH This framework today, in the new object, see the Great God with the persist and did not use Save, the mind is more puzzled, consult the information, found this write very good, reproduced for your reference.

Hibernate save
Hibernate offers too many ways to save objects, and there are a lot of differences between them, and here's a little bit of a different way:
First, the preparation of knowledge:
Before all, for Hibernate, it has three states of objects, transient, persistent, detached
Below is a common translation approach:
Transient: transient or free-form
Persistent: Persistent state
Detached: off-tube or Free State

Instances of the off-state can be persisted by calling the save (), persist (), or Saveorupdate () method.
A persisted instance can become a de-state by calling Delete (). The instances obtained through the get () or load () methods are persisted.
Instances of the off-state can be persisted by calling update (), 0saveOrUpdate (), lock (), or replicate ().

Save () and persist () will cause SQL Insert,delete () to raise Sqldelete,
The update () or merge () throws a sqlupdate. Modifications to the persisted (persistent) instance are detected when the commit is refreshed,
It can also cause sqlupdate. Saveorupdate () or replicate () will cause sqlinsert or update

Second, save and update differences
The reason for putting this pair first is because the pair is the most common.
The role of Save is to save a new object
Update is to save an object that is out of the tube state

Third, update and saveorupdate differences
This is a better understanding, as the name implies, Saveorupdate is basically the synthesis of save and update
Refer to a passage in hibernate reference to explain their use and differences
Typically the following scenario uses update () or saveorupdate ():
The program loads an object in the first session
The object is passed to the presentation layer
There have been some changes to the object
The object is returned to the business logic layer
The program calls the update () method of the second session to persist these changes

Saveorupdate () do the following:
If the object is already persisted in this session, do nothing
Throws an exception if another object associated with this session has the same persistent identity (identifier)
If the object does not have a persisted identity (identifier) attribute, call Save () on it
If the persistent identity of the object (identifier) indicates that it is a newly instantiated object, call Save () on it
If the object is accompanied by version information (through <version> or <timestamp>) and the value of the Version property indicates that it is a newly instantiated object, save () it.
Otherwise, the update () object

Four, persist and save difference
This is the most blurred pair, which appears to be used on the surface, and there is no clear distinction between them in the Hibernate reference documentation.
A clear distinction is given here. (You can follow SRC to see, although the implementation steps are similar, but there are subtle differences)
Here is a description in http://opensource.atlassian.com/projects/hibernate/browse/HHH-1682:
---------------------------------------------------------------------------------
I found that a lot of people has the same doubt. To the solve this issue
I ' m quoting Christian Bauer:
Anybody finds this thread ...

Persist () is the well defined. It makes a transient instance persistent. However,
It doesn ' t guarantee that the identifier value would be assigned to the persistent
Instance immediately, the assignment might happen at flush time. The spec doesn ' t say
That, which is the problem I has with persist ().

Persist () also guarantees that it won't execute an INSERT statement if it's
Called outside of transaction boundaries. This was useful in long-running conversations
With an extended session/persistence context. A method like persist () is required.

Save () does not guarantee the same, it returns a identifier, and if an INSERT
Have to is executed to get the identifier (e.g. "identity" generator, not "sequence"),
This inserts happens immediately, no matter if you are inside or outside of a transaction. This isn't good in a long-running conversation with an extended session/persistence context. "

---------------------------------------------------------------------------------
Briefly translate the main contents of the above sentence:
1,persist persists a transient instance, but does not guarantee that the identifier is immediately populated into the persisted instance, and that the identifier's fill-in May be deferred
To flush the time.

2,persist "Guarantee" does not trigger a SQL Insert when it is called outside of a transaction, this function is very useful,
When we encapsulate a long session process by inheriting session/persistence context, a function such as persist is needed.

3,save "does not guarantee" 2nd, it returns an identifier, so it executes the SQL insert immediately, whether inside or outside transaction


Five, saveorupdatecopy,merge and update differences
First of all, the merge is used instead of saveorupdatecopy, this is a detailed view here
Http://www.blogjava.net/dreamstone/archive/2007/07/28/133053.html
Then compare update and merge
The role of update says, "Here's a look at the merge
If an instance of the same persistence identity (identifier) exists in the session, overwriting the old persisted instance with the state of the object given by the user
If the session does not have a corresponding persistent instance, try to load from the database, or create a new persisted instance, and finally return the persisted instance
User-given object is not associated with the session, it is still off the tube
The point is the last sentence:
When we use update, the state of the object A that we provide becomes persisted after execution is completed
But when we use the merge, the execution is done, we provide the object A is still out of the tube State, hibernate or new a B, or retrieve
A Persistent object B, and copies all the values of the object a we provide to this B, after execution completes B is a persistent state, and we provide a or managed state

Six, flush and update differences
The difference between the two is good understanding
Update operation is in the off-state object
While flush is an object that operates on a persisted state.
By default, a persistent state object is not required for update, as long as you change the value of the object, waiting for Hibernate flush to automatically
Saved to the database. Hibernate flush occurs in several cases:
1, when some queries are called
When 2,transaction commits,
3, when flush is manually called

Seven, lock and update differences
Update is to turn an object that has changed out of the tube state into a persistent state
Lock is the permanent state of an object that has not changed the state of the de-
Corresponding to change the contents of a record, two different operations:
The procedure for update is:
(1) Change the object of the riser, call Update
The operation steps for lock are:
(2) Call lock to turn the object from the off-tube state to a persistent state--change the contents of the persisted state--wait for flush or flush manually

Reference content:
Http://www.blogjava.net/iamtin/archive/2006/03/06/33910.aspx
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1682
Http://www.redsaga.com/hibernate-ref/3.x/zh-cn/html/objectstate.html

Transferred from: http://www.cnblogs.com/kevin002/archive/2008/08/26/1277109.html

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.