重写equals方法和hashcode方法的作用

重写equals方法和hashcode方法的作用 重写equals方法和hashcode方法的作用
作用:区分同一个类的不同对象是否是同一个对象。
应用:在Hibernate 定义 Model实体类的 联合主键的时候用到。
举例说明:
1.数据库中的表
teacher表,包括三个字段,id、name、level。其中id和name作为联合主键。
2.类
在类的角度,如果想定义一个联合主键,使用多个属性作为实体类的关键字,那么方式为定义一个单独的类,里面包含多个属性,并且将这个类包含在原有类中。
public class Teacher{
    private TeacherKey tk;
    private String level;

    public void setTeacherKey(TeacherKey tk){
        this.tk = tk;
    }

    public TeacherKey getTk(){
        return tk;
    }

    public void setLevel(String level){
        this.level = level;
    }    
    public String getLevel(){
        return this.level;
    }
}
public class TeacherKey{
    private int id;
    private String name;

    public void setId(int id){
        this.id = id;
    }
    public int getId(){
        return this.id;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }

    public boolean equals(Object o){
        if(o.instanceof  TeacherKey){
            Teacherkey tk = (TeacherKey)o;
            if(this.id == tk.getId() && this.name.equals(tk.getName())){
                return true;
            }
        }
        return false;
    }

    public int hashCode(){
        return this.name.hashCode();
    }
}

3.项目中用到的代码示例:
package iq.model.user;
import java.io.Serializable;
public class User implements Serializable {
    private String id;
    private String name;
    private String password;
    private String email;
    private String mobile;
    private String desc;
    private String type;

    private String state;

    private static final long serialVersionUID = 1L;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id == null ? null : id.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile == null ? null : mobile.trim();
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc == null ? null : desc.trim();
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type == null ? null : type.trim();
    }

    public String getState() {
        return state;
    }


    public void setState(String state) {
        this.state = state == null ? null : state.trim();
    }

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        User other = (User) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getName() == null ? other.getName() == null : this.getName().equals(other.getName()))
            && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()))
            && (this.getEmail() == null ? other.getEmail() == null : this.getEmail().equals(other.getEmail()))
            && (this.getMobile() == null ? other.getMobile() == null : this.getMobile().equals(other.getMobile()))
            && (this.getDesc() == null ? other.getDesc() == null : this.getDesc().equals(other.getDesc()))
            && (this.getType() == null ? other.getType() == null : this.getType().equals(other.getType()))
            && (this.getState() == null ? other.getState() == null : this.getState().equals(other.getState()));
    }


    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
        result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());
        result = prime * result + ((getEmail() == null) ? 0 : getEmail().hashCode());
        result = prime * result + ((getMobile() == null) ? 0 : getMobile().hashCode());
        result = prime * result + ((getDesc() == null) ? 0 : getDesc().hashCode());
        result = prime * result + ((getType() == null) ? 0 : getType().hashCode());
        result = prime * result + ((getState() == null) ? 0 : getState().hashCode());
        return result;
    }
}

上一篇:Java EE开发四大常用框架


下一篇:rowid和rownum有什么不同?