What is the difference between an object in a Free state and an object in a transient state?
Source: Internet
Author: User
What is the difference between an object in a Free state and an object in a transient state. Answer one:
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 ("amigo", "female");
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 SES nsion associated with a persistent 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.
Answer two:
Hibernate object state
Instantaneous (Transient)-the object created by the new operator and not yet associated with the hibernate Session is considered instantaneous (Transient). Instantaneous (Transient) objects are not persisted to the database and are not given persistent identities (identifier). If an instantaneous (Transient) object is not referenced in the program, it is destroyed by the garbage collector (garbage collector). Hibernate session can be used to change it to a persistent (persistent) state. (Hibernate will automatically execute the necessary SQL statements)
Persistent (persistent)-persistent (persistent) instances have corresponding records in the database and have a persistent identity (identifier). An instance of persistence (persistent) may have just been saved, or just loaded, regardless of which, by definition, it exists in the context of the associated session. Hibernate detects any changes to an object that is in a persistent (persistent) State and synchronizes the object data (state) with the database (synchronize) when the current operating unit (unit of work) finishes executing. Developers do not need to perform update manually. Changing an object from a persistent (persistent) state to an instantaneous (Transient) state also does not require that the DELETE statement be executed manually.
Off-pipe (Detached)-when the session associated with a persistent (persistent) object is closed, the object becomes de-(Detached). The reference to the Detached object is still valid and the object can continue to be modified. If the Detached object is re-associated to a new session, it will be converted again to persistent (persistent) (changes in the Detached will be persisted to the database). This feature makes it possible for a programming model to be programmed with a long-running operating unit (unit of work) in which the user thinks time is think-time. We call it an application transaction, that is, from the user's point of view, an operating unit (unit of work).
The top says the escrow is the Free State #|hibernate| Report view (288) Comments Reproduced
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.