Why does hibernate advocate persistence classes to implement equals () and hashCode ()?

Source: Internet
Author: User

If you have used Set, you will know that duplicate values are not allowed to be stored in the Set. This is why Hibernate recommends using Set as the storage object in Multi-Table Association ing. The following code is common in persistent classes in Multi-Table Association ing:

Private Set <?> SysUsersRoles = new HashSet (0 );
Private Set <?> SysRolesAuthorities = new HashSet (0 );

Public Set getSysUsersRoles (){
Return this. sysUsersRoles;
}

Public void setSysUsersRoles (Set sysUsersRoles ){
This. sysUsersRoles = sysUsersRoles;
}

Public Set getSysRolesAuthorities (){
Return this. sysRolesAuthorities;
}

Public void setSysRolesAuthorities (Set sysRolesAuthorities ){
This. sysRolesAuthorities = sysRolesAuthorities;
}

According to Hibernate, The equals () and hashCode () methods should be rewritten in this persistence class. So why do we need to rewrite these two methods? Isn't repeated values allowed in Set?
What is the role of rewriting these two methods? What is the meaning of equals () and hashCode?

This is my question after I saw the rewrite methods advocated by Hibernate!

Let's take a look at the functions of equals () and hashCode.

In fact, the equals () and hashCode () methods exist to distinguish objects. Both methods are from the Object class.

During object running, to differentiate objects at runtime, the difference between the two methods is as follows:

1. The public boolean equals (Object obj) method of the Object class is to return this = obj; this method compares whether the two objects are the same. Here the reference is compared.
2. The public int hashCode () method of the Object class is to convert the instance address to the int value. Therefore, the hashCode of different Object instances must be different during the same runtime.

Now we have solved the question of what equals () and hashCode () are and what their functions are.

Let's talk about the running mechanism of Hibernate:
During the runtime of Hibernate, the list of objects extracted through find or other methods can be operated in different contexts, or in the transformation of the instantaneous, persistent, and unmanageable states, to avoid confusion caused by instances with the same class name but different object content colliding with each other, you need to use a more accurate method to differentiate objects. At this time, equals () and hashCode () these two methods cannot meet the requirements. Only by rewriting these two methods and adding a distinction between the content of each attribute of these persistent classes can we truly distinguish the objects extracted from the find method of Hibernate.

So what types of instances are the same? Of course, in addition to your persistent class name, you also need to clearly identify that the values of some attributes in the persistent instance are also equal, so that the two instances are truly the same.

For example, the following two rows of data are from the Person table (the Field content includes ID, name, age, and father name fartherName). This is an example of the same instance:

1. Wang Changjiang, 30, Wang Youcai;
1. Wang Changjiang, 30, Wang Youcai;

The following two line instances are different examples:
1. Wang Changjiang, 30, Wang Youcai;
1. Wang Changjiang, 30, Wang Youcai;

Haha, do you see the difference? If there is only one word difference, it will be regarded as two instances in the instance managed by Hibernate. This is the important role of rewriting hashCode.

By the way, write the rewritten hashCode () and equals () below:

Public boolean equals (Object other ){

If (this = other) return true;

If (! (Other instanceof Person) return false;

Final Person person = (Person) other;

If (! Person. getName (). equals (getName () return false;

If (! Person. getAge (). equals (getAge () return false;

If (! Person. getFartherName (). equals (getFartherName () return false;

Return true;

}

Public int hashCode (){
Int result;

Result = getName (). hashCode ();

Result = 29 * result + getAge ();

Result = 29 * result + getFartherName ();

Return result;
}

Also, you can type the following code to try the difference:

Public static void main (String [] args ){

SysRoles role1 = new SysRoles ("1", "lxb", "l1 ");
SysRoles role2 = new SysRoles ("1", "lxb", "l1 ");

/**//*
* After the test, if equals and hashCode are not rewritten, false is displayed;
* True is displayed during rewriting.
* This is why equals and hashCode are rewritten. When you want to extract objects from the hiberate instance,
* If the content of all fields is the same, the two object instances are considered to be the same. In this case, you need to rewrite equals and hashCode.
* Rewriting equals and hashCode means that the two instance objects in different contexts and sessions have fixed semantics.
*/
System. out. println (role1.equals (role2 ));

/**//*
* After the test, if equals and hashCode are not rewritten, false is displayed;
* True is displayed during rewriting.
*
*/
System. out. println (role1.hashCode () = role2.hashCode ());

}

Haha, let's summarize it!


 

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.