Proficient Hibernate--java memory address with Hibernate's built-in object identifiers

Source: Internet
Author: User

In the Java language, the two object reference variables are judged to be equal and have the following two comparison methods:
(1) Compare the memory addresses of the objects referenced by the two variables, and "= =" is the memory address of the comparison. In addition, the Equals (object o) defined in the object class is also compared by memory address. If the user-defined class does not overwrite the Equals (Object O) method, it is also compared by memory address. For example, the following code creates a total of two customer objects with a new statement, and defines a reference variable of three customer types, c1,c2,c3:

new Customer("Tom"new Customer("Tom‘);Customer c3 = c1;c3.setName("Mike");

The program executes to the third line as follows:

Execute to line fourth

the C1 and C3 variables refer to the same customer object, and the C2 variable references another customer object, so the expression C1 = = Both C3 and C1.equals (C3) are true, and the expression C1 = = C2 and C1.equals (C2) are false.
(2) compares the values of the objects referenced by the two variables, and some classes in the Java API override the Equals method of the object class for comparison by value, including:
String class and date class. The
Java wrapper class, including: Byte, Integer, short, Character, Long, Float, Double, and Boolean.
For example:
As can be seen from the above two graphs, the C1 and C3 variables refer to the same customer object, and the C2 variable references another customer object, so the expression C1 = = C3 and C1.equals (C3) are true, and the expression C1 = = Both C2 and C1.equals (C2) are false.
(2) compares the values of the objects referenced by the two variables, and some classes in the Java API override the Equals method of the object class for comparison by value, including:
String class and date class. The
Java wrapper class, including: Byte, Integer, short, Character, Long, Float, Double, and Boolean.
For example:

StringnewString("hello");StringnewString("hello");

Although S1 and S2 refer to different string objects, their string values are Hello, so the expression S1==s2 is false and the value of s1.equals (S2) is true.
User-defined classes can also override the Equals (object O) method of the object class to achieve comparison by object values. For example, in the Customer class, add the following equals (Object O) method so that it compares the two customer objects for equality by the client's name:

public   Boolean  equals  (Object o) {if  (this  = = O) return  true ; if         (!o instanceof  Customer) return     false ; final     Customer other = (customer) O; if         (this . GetName (). Equals (Other.getname ())) return     true ;         else  return  false ;}  

The following code creates a total of two customer objects with the new statement, and defines two reference variables for the customer type C1 and C2

new Customer("Tom"new Customer("Tom");

Although C1 and C2 refer to different customer objects, their name value is tome, so the value of the expression C1 = = C2 is False, and the value of the expression c1.equals (C2) is true.
Identify or distinguish different objects of the same class in Java Speech by memory address, and close in Hibernate with Object identifiers (OIDs) to differentiate objects. An OID is the equivalent of a primary key (typically a surrogate primary key) in a relational database in the Java object model. Hibernate maintains the correspondence between Java objects and records in database tables at run time, based on OIDs. For example:

Transaction tx = Session. BeginTransaction();Customer C1 = (Customer) session. Load(Customer. Class, New Long (1));Customer C2 = (Customer) session. Load(Customer. Class, New Long (1));Customer C3 = (Customer) session. Load(Customer. Class, New Long (3));System. out. println(C1==C2);System. out. println(C1==C3);Tx. Commit();

In the above program, three times the load method of the session is called, respectively, the OID is 1 or 3 of the Customer object, the following is hibernate three times the process of loading the customer object.
(1), when the first load of the OID 1 of the Customer object, first from the data of the Customer table to query the record ID 1, and then create the corresponding customer instance, save it in the session cache summary, and finally the object to assign a reference to the variable C1
(2), when the second load of the OID is 1 of the customer object, directly to the cache of the OID is 1 of the Customer object's reference is assigned to C2, so C1 and C1 reference the same customer object
(3), when the load OID is 3 of the customer object, because there is no such object in the cache, you must again to the database to query the customer Object ID 3, and then create the corresponding customer instance, save him in the session cache, Finally, I assign a reference to this object to the variable C3.
Therefore, the expression C1 = = C2 is true,c1==c3 to false.
Corresponding to the table's proxy primary key, the OID is also an integer type, and Hibernate allows the OID to be defined as the following integer type in the persistent class.
Shot (or wrapper class short): 2 byte range -2^15 ~ 2^15-1
int (or wrapper class integer): 4 byte value range -2^31~ 2^31-1
Long (or wrapper type long): 8 byte range -2^63 ~ 2^63-1
In an object-relational mapping file, an element is used to set an object identifier such as:

<idname="id" type="long" column="ID">    class="increment" /></id>

The child element is used to set the identity Fuzhou generator. Hibernate provides the identifier generator interface: The Identifiergenerator interface, and provides a variety of built-in implementations. Here are a few simple examples:
Increment:hibernate automatically increments the identifier, each increment is 1
Identity: Generates an identifier from the underlying database. The prerequisite is that the underlying database supports autogrow field types
Sequence: Generates identifiers based on the sequence of the underlying database, provided the underlying database supports sequences
Hilo:hibernate generates identifiers based on the High/low algorithm, Hibernate takes the fields of a particular table as the high value, and by default chooses the Next_hi field of the Hibernate_unique_key table
Native: Select identity, Sequence, or Hilo based on the support capabilities of the underlying database for auto-generated identifiers
Assigned: For natural primary keys, the Java application is responsible for generating identifiers, in order to allow the Java application to set the OID, cannot declare the SetID () method as private, should try to avoid the use of natural primary key.

Proficient Hibernate--java memory address with Hibernate's built-in object identifiers

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.