Hibernate the Persistent object

Source: Internet
Author: User
Tags garbage collection

Hibernate is a pure o/rmapping framework, with hibernate support, program developers only need to manage the status of objects, regardless of the underlying database system details. The equivalent of a common JDBC persistence layer scenario requires the manual management of SQL statements, which hibernate to manipulate the database in a fully object-oriented manner. For program developers, there are only objects and attributes in the eyes, ignoring the concepts of the underlying data tables and data columns. 1. Requirements for the persistence of classes

Although the hibernate is too far away from the persistent class, the following rules should be followed:

1 provides a constructor without parameters: All the persisted classes should provide a constructor without parameters, and this constructor may not take the public access control character. As long as you provide a parameterless constructor, hibernate can use Constructor.newinstance () to create an instance of the persistent class. Typically, in order to facilitate hibernate generation of proxies at runtime, the access control parameter character of the constructor is at least visible, which is greater than or equal to the default access control character.

2 provides an identity attribute: The IDENTITY property typically maps the primary key field of the database. This property can be called any name, and its type can be any basic type, basic type of wrapper, java.lang.Sring or java.util.Date. If you use a federated primary key for a database table, you can even use a user-defined class that has properties of those types. Of course, it is also possible to not specify any identity attributes, but rather to map multiple common objects directly into a federated primary key in persistent annotations, but this is not usually recommended.

Hibernate recommends that you use nullable types as the type of identity attribute, because you should avoid using basic data types as much as possible.

1 provides a setter and getter method for each member variable of the persisted class: Hibernate to access the persisted member variable by default.

2 Use a non final class: Generating an agent at run time is an important function of hibernate. If the persistent class does not implement any interfaces, Hibernate uses the Javassist build agent, which is an instance of a subclass of the persisted class. If the final class is used, the javassist agent cannot be generated and performance optimization is not possible. If you do not want to use a class that has the public final method, you must explicitly disable the proxy by setting lazy= "false".

3 rewrite the equels () and Hashcode () method: If you need to put an instance of a persisted class into set, you should override the Equals () and Hashcode () methods for the persisted class. The most obvious way to implement Equals ()/hashcode () is to compare the values of two object identity attributes.

2. The state of the persisted object

In the previous chapter, the author has introduced the persistence, so here we go deep to understand a persistence. The Hibernate persistence object supports the following States.

1 Transient: Objects created by the new operator, and objects that have not been associated with the hibernate session are considered transient. Transient objects are not persisted to the database and are not given persistent identities. If a transient object reference is lost in the program, the transient object is destroyed by the garbage collection mechanism. Using the Hibernate session may change it to a persisted state.

2 Persistence: Persistent instances have corresponding records in the database and have a persistent identity (idebtifier). The persisted instance may have just been saved, or it may have just been loaded. Either way, the persisted object must be associated with the specified hibernate session. Hibernate detects changes to the persisted state object and writes the object data back to the database when the current operation completes. Developers do not need to perform update manually.

3) off: an instance is once in a persistent state, but as the session associated with it is closed, the object becomes a de-tube state. The reference to the off-pipe object is still valid and the object can continue to be modified. If you reconnect the off-pipe object to the session, the off-pipe object is converted to a persistent state, and the change during the removal period is not lost or written to the database. It is because of this function that logically long transactions become possible, it is called application transactions, that is, transactions can be considered across the user, because when the object is out of control, the operation of the object without locking the database, does not cause performance degradation.

The Hibernate Persistence object Evolution diagram is shown below.


3. Summary

Understanding the persistence of hibernate three states, I help myself to understand the state of JavaBean objects, and their own operations on the data. The program can also be better optimized in operations. Of course, for the method of persisting the state of the object, we will explain it in the next section. Before that, it is hoped that the reader will have a better understanding of the three states of persistence.

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.