Hibernate Save Update Merge

Source: Internet
Author: User
Tags commit manual flush

There are 3 types of Hibernate objects: Transient (Transient), persistent (persistent), and off-state (Detached). Objects that are in persistent state are also known as PO (Persistence object), and instantaneous and de-tube objects are also known as VO (Value object).

Instantaneous state
A Java object that opens up memory space by the new command,

eg. person person = new person ("xxx", "xx");

If the object is not referenced by a variable, it will be reclaimed by the Java virtual machine.

Instantaneous objects exist in memory, it is the carrier that carries the information, does not have any relation with the database data, in Hibernate, the instantaneous object can be associated with the database through the session's save () or Saveorupdate () method, and the data corresponding to the inserted database , the instantaneous object is transformed into a persisted object.

Persistent state
The object in that state has a corresponding record in the database and has a persistent identity. In the case of Hibernate's Delete () method, the corresponding persistent object becomes instantaneous, because the corresponding data in the database has been deleted, and the object is no longer associated with the database's record.

When a session executes close () or clear (), evict (), the persisted object becomes a de-tube object, at which point the persisted object becomes a de-tube object, while the object has a database recognition value, but it is not under the management of Hibernate persistence layer.

Persistent objects have the following characteristics:

1. Associate with the session instance;

2. There is a record associated with it in the database.

Off-Pipe State
When the session associated with a persisted object is closed, the persisted object is turned into a de-tube object. When the off-tube object is re-associated to the session, it is again transformed into a persistent object.

The de-tube object has the recognition value of the database and can be transformed into a persistent object by means of update (), Saveorupdate () and so on.

The off-pipe object has the following characteristics:

1. Essentially the same as an instantaneous object, when no variable references it, the JVM will recycle it at the appropriate time;

2. More than the instantaneous object a database record identification value.


Hibernate's various Save methods (Save,persist,update,saveorupdte,merge,flush,lock) and three states of the object
Hibernate save
Hibernate offers too many ways to save objects, and there are a lot of differences between them.
I. Preliminary knowledge
for Hibernate, its objects have three states, transient, persistent, detached
Below are common translation methods:
Transient: transient or Free State
(new DEPTPO (1, "Administrative department", 20, "administrative related"), the instance of the PO is not associated with the session, the instance of the PO is in transient)
persistent: persisted state
(and the database records the PO instance that you want to insinuate, Its state is persistent, the object obtained through get and load is persistent)
detached: off-state or Free State
(1) When the PO objects obtained by the Get or Load method are in persistent, However, if the Delete (PO) is executed (but the transaction cannot be performed), the PO status is detached, (indicating that the session is out of association), and a free state can be changed by Save or saveorupdate () to a persistent state
(2) When the session is closed, the persistent PO object in the session cache becomes detached
can be changed to a persistent state by lock, save, and update because the session is closed by a free State
A persisted instance can become a de-state by calling Delete (). The
instances obtained through the get () or load () methods are persisted. An instance of the
off-state can be persisted by calling lock () or replicate ().

Save () and persist () will cause SQL Insert,delete () to raise Sqldelete,
The update () or merge () throws a SQL update. Modifications to a persisted (persistent) instance are detected when the commit is refreshed, and it also causes SQL UPDATE.
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 a de-state object or a Free state object (be sure to correspond to a record) to the database

Iii. Update and Saveorupdate differences
This is a good understanding, as the name implies, Saveorupdate basically is the synthesis of save and update, and update is only update; Refer to a paragraph in Hibernate reference to explain their use and differences
Typically the following scenario uses update () or saveorupdate ():
The program loads the object in the first session and then closes the 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 eventually to the persistence layer
The program creates a second session call to the second session of the update () method to persist these changes

Saveorupdate (PO) do the following:
If the PO object is already persisted in this session, perform saveorupdate in this session without doing anything
If the Savaorupdate (New PO) has the same persistent identity (identifier) as another PO object associated with this session, an exception is thrown
Org.hibernate.nonuniqueobjectexception:a different object with the same identifier value is already associated with the session: [Org.itfuture.www.po.xtyhb#5]
Saveorupdate if the object does not have a persisted identity (identifier) attribute, call Save () on it, otherwise the update () object

Iv. differences between persist and save
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)
Main content differences:
1,persist persists a transient instance, but does not guarantee that the identifier (the attribute corresponding to the identifier primary key) is immediately populated into the persisted instance, and that the identifier filling may be deferred until flush.

2,save, the persistent identifier of a transient instance, is generated in a timely manner, it returns an identifier, so it executes the SQL insert immediately

V. Saveorupdate,merge and UPDATE differences
Compare update and Merge
The role of update says, "Here's a look at the merge
If there is an instance of the same persistent identity (identifier) in the session, overwrite the persisted instance of the session with the object given by the user
(1) When we use update, we will throw an exception when we finish the execution.
(2) But when we use the merge, we copy the properties of the Po object A that handle the free State to the properties of the PO that is in the persistent state in the session, whether it is persistent or persistent after execution, and we provide a or a free State

Six, flush and update differences
The difference between the two is good understanding
The update operates on an object that is in a free State or in a de-//updatesql state (in the off-pipe state due to the closing of the session).
While flush is an object that operates on a persisted state.
By default, changes to a persistent state object (including the set container) do not require an update, as long as you change the value of the object, waiting for Hibernate flush to automatically update or save to the database. Hibernate flush occurs in the following situations:
1, call some queries and manual flush (), session closed, Sessionfactory closed combined
Get () An object that changes the properties of the object and closes the resource.
2,transaction commit (contains flush)

Seven, lock, and update differences
Update is to make a persistent state of an object that has been changed from a de-tube state
Lock is a persistent state for an object that has not changed out of the tube state (for a PO object that is in the off state due to the closing of the session) (2), can not be in the removal of the PO object because of the delete)
corresponding to change the contents of a record, two different operations:
Update the procedure is:
(1) property changes after the removal of the object modified, call Update
The procedure for lock is:
(2) Call lock to change the unmodified object from the off-state to a persistent state--the contents of the object that changed the persisted state--wait for flush or manual flush
Viii. The difference between clear and evcit
Clear full Clear session cache
Evcit (obj) empties a persisted object from the session's cache.

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.