Analysis of three states of Hibernate mapping objects

Source: Internet
Author: User

First, there are three types of objects in Hibernate: Transient, Free State, and persistent state, three methods of state conversion are called by session, transient to persistent state is save (), Saveorupdate (), get (), load () , persistent to transient method has delete (), the method of free State to persistent state has update (), Saveorupdate (), Lock (), the method of persistent state to Free State is: Session.close (), Session.evict (), Session.clear ().

Two, Hibernate's status

Hibernate's various preservation 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.

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 state or temporary state

(New Deptpo (1, "Administrative department", 20, "administrative related"), the instance of the PO is not associated with the session, and the instance of the PO is in transient)

Persistent: Persistent state

(and the database records the PO instance that you want to insinuate, its state is persistent, the objects obtained by get and load are all persistent)

Detached: off-tube or Free State

(1) When the PO objects obtained through the Get or load methods are persistent, but if the delete (PO) is executed (but the transaction cannot be performed), the PO status is detached, (indicating and session out of association), A Free state due to delete can be persisted by Save or saveorupdate ()

(2) When the session is closed, the persistent PO object in the session cache becomes detached

A free state can be changed into a persistent state by lock, save, and update, because the session is closed.

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 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 turn an object that has changed out of the tube state into a persistent state

Lock is a persistent state of a non-changed off-state object (for a PO object (2) that is in the de-state due to the closing of the session, and not for a PO object that is in a de-state due to delete)

Corresponding to change the contents of a record, two different operations:

The procedure for update is:

(1) Modification of the off-tube object after a property change, call Update

The operation steps for lock are:

(2) Call lock to change an unmodified object from the off-pipe state to a persistent state--changing the contents of the persisted state--waiting for flush or manually flush

Viii. the difference between clear and evcit

Clear full Erase session cache

Evcit (obj) empties a persisted object from the session's cache.

Session.lock (Xtyhb,lockmode.none);//means to go directly to the cache to find the object that becomes persistent state

Session.lock (Xtyhb,lockmode.read);//The ID of the record is first read through the ID of the database to see if there is a record, if there is a subsequent to the cache to find the object becomes persistent state





In hibernate, there are three states of objects: temporary, persistent, and free.

also known as: Transient (Transient), persistent (persistent), 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).

Temporary state: When new is a physical object, the object is in a temporary state, that is, an area of memory that holds temporary data, and if no variable references the object, it is reclaimed by the JRE garbage collection mechanism. The data stored by this object has no relation to the database, except that the object is converted to a persistent object by using the session's Save or saveorupdate to associate the temporary object with the database and inserting or updating the data into the database.
Example: EMP e=new emp ();   //Create temporary Object
          E.setempno ((long) 8888);
          E.setename ("Mike");
          ...
          Empdao d=new Empdao ();
          D.save (E);     //Persistence
          ...

Persistent state: an instance of a persisted object has a corresponding record in the database and has a persistent representation (ID). After a delete operation on a persisted object, the corresponding record in the database is deleted, and the persisted object no longer has a corresponding relationship with the database record, and the persisted object becomes a temporary state.
After the persisted object is modified, it is not synchronized to the database immediately, knowing that the database transaction commits. The persisted object is dirty (Dirty) before synchronization.
For example:
EMP e=new EMP (); A temporary object was created
Empdao edao= new Empdao ();
E=edao.findbyempno ((long) 7786); Persist objects to database records
E.setename ("new name"); The persisted object was modified to be in Dirty
......
Edao.saveorupdate (e); Save, but still Dirty
Tran.commit (); Commit, synchronize with database, no longer dirty
......

Free State: When the session is closed, clear, or evict, the persisted object has a persistent identifier and a value consistent with the database corresponding record, but because the session has disappeared, the object is not within the persistence management, so it is in a free State (also known as a "de-tube" state). An object with a free state is very similar to a temporary state object, except that it also contains persistent identities.


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.

Analysis of three states of Hibernate mapping objects

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.