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

简介:  重写equals方法和hashcode方法的作用 作用:区分同一个类的不同对象是否是同一个对象。 应用:在Hibernate 定义 Model实体类的 联合主键的时候用到。 举例说明: 1.数据库中的表teacher表,包括三个字段,id、name、level。其中id和name作为联合主键。 2.类在类的角度,如果想定义一个联合主键,使用多个属性作为实
  重写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;
    }
}

相关文章
|
SQL Java 关系型数据库
调优为王!阿里巴巴彩版java性能调优实战,终于到手了!文末福利
开始之前,我先来讲一下我对性能调优的看法。在我看来Java的性能调优并不是像学习编程语言一样可以通过学习掌握,它是没有办法用直线的思维学会并掌握使用的,并且它对于程序员来说,对技术深度和广度有这十分高的门槛。
|
存储 消息中间件 Kafka
ClickHouse 23.8 (LTS) 版本发布说明
以下是ClickHouse 23.8 (LTS) 版本一些亮点功能...这次发布涵盖了向量的算术运算、tuple的连接、cluster/clusterAllReplicas的默认参数、从元数据中计数(对于Parquet来说速度提高了5倍)、文件内跳数(对Parquet有巨大提升)、从对象存储中流式消费数据,等等
|
11月前
|
Java 开发者 Spring
深入解析:Spring AOP的底层实现机制
在现代软件开发中,Spring框架的AOP(面向切面编程)功能因其能够有效分离横切关注点(如日志记录、事务管理等)而备受青睐。本文将深入探讨Spring AOP的底层原理,揭示其如何通过动态代理技术实现方法的增强。
365 8
|
11月前
|
安全 Go 数据处理
Go语言中的并发编程:掌握goroutine和channel的艺术####
本文深入探讨了Go语言在并发编程领域的核心概念——goroutine与channel。不同于传统的单线程执行模式,Go通过轻量级的goroutine实现了高效的并发处理,而channel作为goroutines之间通信的桥梁,确保了数据传递的安全性与高效性。文章首先简述了goroutine的基本特性及其创建方法,随后详细解析了channel的类型、操作以及它们如何协同工作以构建健壮的并发应用。此外,还介绍了select语句在多路复用中的应用,以及如何利用WaitGroup等待一组goroutine完成。最后,通过一个实际案例展示了如何在Go中设计并实现一个简单的并发程序,旨在帮助读者理解并掌
|
12月前
|
JavaScript BI 数据处理
computed 与 method 结合使用的示例
【10月更文挑战第15天】Computed 与 Method 的结合使用为 Vue 应用的开发提供了更多的可能性和灵活性。通过合理运用,可以更好地处理数据计算和逻辑操作,提高应用的性能和可维护性。在实际开发中,要根据具体需求和场景,巧妙地将两者结合起来,以达到最佳效果。
178 3
|
监控 安全 网络协议
【网络工程师必备神器】锐捷设备命令大全:一文在手,天下我有!
【8月更文挑战第22天】锐捷网络专攻网络解决方案,其设备广泛应用在教育、政府及企业等领域。本文汇总了锐捷设备常用命令及其应用场景:包括登录与退出设备、查看系统状态、接口与VLAN配置、路由与QoS设定、安全配置及日志监控等。通过示例如telnet/ssh登录、display命令查看信息、配置IP地址与VLAN、设置静态路由与OSPF、限速与队列调度、端口安全与ACL、SNMP监控与重启设备等,助力工程师高效管理与维护网络。
1013 4
|
存储 容器
shiro配置路径为anno,但请求还是拦截下来了
shiro配置路径为anno,但请求还是拦截下来了
765 2
|
存储 缓存 JavaScript
三个小时vue3.x从零到实战(前)(vue3.x基础)
该文章系列提供了Vue3.x从基础到实战的教程,涵盖安装、基本语法、组件化应用及项目构建等多个方面,适合从零开始学习Vue3.x的开发者。
1092 0
|
弹性计算 关系型数据库 MySQL
|
SQL Java Maven
使用阿里云的 flink-connector-clickhouse 插件
使用阿里云的 flink-connector-clickhouse 插件
1174 2