Proficient in Hibernate-Java memory address and built-in object identifier of Hibernate
In Java, the following two methods are used to determine whether the referenced variables of two objects are equal:
(1) compare whether the memory addresses of the objects referenced by the two variables are the same, 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 according to the memory address. For example, the following code creates two Customer objects with the new statement and defines three Customer-type reference variables, c1, c2, and c3:
Customer c1 = new Customer("Tom");Customer c2 = new Customer("Tom');Customer c3 = c1;c3.setName("Mike");
The program runs to the third line as follows:
Execute to the fourth line
From the above two figures, we can see that C1 and C3 variables reference the same Customer object, while C2 variables reference another Customer object. Therefore, the expressions C1 = C3 and C1.equals (C3) are true, the expressions C1 = C2 and C1.equals (C2) are both false.
(2) compare whether the values of the objects referenced by the two variables are the same. Some Classes in Java API overwrite the equals method of the Object class to achieve value-based comparison, including:
String class and Date class.
Java packaging class, including Byte, Integer, Short, Character, Long, Float, Double, and Boolean.
For example:
From the above two figures, we can see that C1 and C3 variables reference the same Customer object, while C2 variables reference another Customer object. Therefore, the expressions C1 = C3 and C1.equals (C3) are true, the expressions C1 = C2 and C1.equals (C2) are both false.
(2) compare whether the values of the objects referenced by the two variables are the same. Some Classes in Java API overwrite the equals method of the Object class to achieve value-based comparison, including:
String class and Date class.
Java packaging class, including Byte, Integer, Short, Character, Long, Float, Double, and Boolean.
For example:
String s1 = new String("hello");String s2 = new String("hello");
Although s1 and s2 reference different String objects, their String values are "hello", so the expression s1 = s2 is false, and the value of s1.equals (s2) is true.
The user-defined class can also overwrite the equals (Object o) method of the Object class, so as to achieve comparison by Object value. For example, add the following equals (Object o) method to the Customer class to compare whether two Customer objects are equal based on the Customer 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 two Customer objects with the new statement and defines the reference variables c1 and c2 of the two Customer types.
Customer c1 = new Customer("Tom");Customer c2 = new Customer("Tom");
Although c1 and c2 reference different Customer objects, their name values are all Tome, so the value of expression c1 = c2 is false, and the value of expression c1.equals (c2) is true.
In Java speech, different objects of the same class are identified or distinguished by memory addresses. In Hibernate, Object Identifiers (OID) are used to distinguish objects. OID is the equivalent of the primary key (usually the proxy primary key) in the relational database in the Java object model. At runtime, Hibernate maintains the correspondence between Java objects and records in database tables based on the OID. 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, the Session load method is called three times to load the Customer object whose OID is 1 or 3, respectively. The following is the process for Hibernate to load the Customer object three times.
(1) when loading the Customer object with OID 1 for the first time, first query the record with ID 1 in the Customer table of the data, and then create the corresponding Customer instance, save it in session cache summary, and assign the reference of this object to the variable c1
(2) When the Customer object whose OID is 1 is loaded for the second time, the reference of the Customer object whose OID is 1 in the cache is directly assigned to c2. Therefore, c1 and c1 reference the same Customer object.
(3) When loading a Customer object with OID 3, because such an object does not exist in the cache, you must query the Customer object with ID 3 again in the database, create a Customer instance, save it in the Session cache, and assign the reference value of this object to the variable c3.
Therefore, the expression c1 = c2 is true, and c1 = c3 is false.
Corresponds to the proxy primary key of the table. OID is also an integer type. Hibernate allows you to define OID as the following Integer type in the persistent class.
Shot (or packaging class Short): The value range of 2 bytes-2 ^ 15 ~ 2 ^ 15-1
Int (or packaging Integer): 4 bytes value range-2 ^ 31 ~ 2 ^ 31-1
Long (or Long packaging class): 8 bytes; value range:-2 ^ 63 ~ 2 ^ 63-1
In the object-link ing file, elements are used to set object identifiers, for example:
The child element is used to set the identifier generator. Hibernate provides the IdentifierGenerator interface and multiple built-in implementations. Below are several simple introductions:
Increment: Hibernate automatically increments to generate an identifier. Each increment is 1.
Identity: The identifier generated by the underlying database. The prerequisite is that the underlying database supports automatic field growth.
Sequence: generates identifiers Based on the sequence of the underlying database, provided that the underlying database supports the sequence
Hilo: Hibernate generates identifiers based on the high/low algorithm. Hibernate uses the fields in a specific table as the high value. By default, the next_hi field of the hibernate_unique_key table is used.
Native: Based on the ability of the underlying database to automatically generate identifiers, choose identity, sequence, or Hilo
Assigned: applicable to natural primary keys. The Java application is responsible for generating identifiers. To allow Java applications to set OID, The setId () method cannot be declared as private. Avoid using natural primary keys whenever possible.