Write method for equals () and hashcode () methods in Hibernate

xiaoxiao2021-03-06  47

The equals method is to determine if the PO is equally important. Of course, the requirements of the Equals method are also required to overwrite the HashCode method, and the following instance is a general manner over the PO.

Package test.base;

Import java.io.serializable;

Public Abstract Class UserInfo IMPLEments Serializable {

Private int.come = integer.min_value;

// primary key private java.lang.string userid;

// Fields private java.lang.string username; private java.lang.string useeraccount;

Public userInfo () {}

Public userInfo (java.lang.string userid) {this.setUserid (userID);

Public Java.lang.String getUserid () {return userid;}

Public void setUserid (java.lang.string userid) {this.userid = userid; // Because the HashCode () method is related to the primary key value, restore the HashCode value when resetting the primary key. this.hashcode = integer.min_value; }

Public java.lang.string getUsername () {return this.username;

Public void setusername (java.lang.string username) {this.username = usrname;}

Public Java.lang.String getuseraccount () {return this.useraccount;}

Public void setUseraccount (java.lang.string useeraccount) {this.useraccount = useercount;}

Public boolean equals (object obj) {if (null == obj) Return false; if (! (!) Return False; else {test.base.userinfo mobj = (Test.base.userinfo) Obj; if (null == this.getuserid () || null == mobj.getuserid ()) Return False; Else Return (this.getuserid (). Equals (mobj.getuserid ()));}}

Public int hashcode () {// This purpose is that the HashCode value only needs to be calculated. if (Integer.min_Value == this.hashcode) {if (null == this.getuserid ()) Return super.hashcode (); Else {string hashstr = this.getClass (). getname () ":" this.getuserid (). hashcode (); this.hashcode = hashstr.hashcode ();}} returnimudde ();}}

}

转载请注明原文地址:https://www.9cbs.com/read-80291.html

New Post(0)