当前位置:网站首页>@EqualsAndHashCode注解的使用
@EqualsAndHashCode注解的使用
2022-07-15 16:07:00 【会飞的猫不吃鱼】
官方文档:@EqualsAndHashCode
原文中提到的大致有以下几点:
此注解会生成equals(Object other) 和 hashCode()方法。
它默认使用非静态,非瞬态的属性
可通过参数exclude排除一些属性
可通过参数of指定仅使用哪些属性
它默认仅使用该类中定义的属性且不调用父类的方法
可通过callSuper=true解决上一点问题。让其生成的方法中调用父类的方法。
另:@Data相当于@Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode这5个注解的合集。
通过官方文档,可以得知,当使用@Data注解时,则有了@EqualsAndHashCode注解,那么就会在此类中存在equals(Object other) 和 hashCode()方法,且不会使用父类的属性,这就导致了可能的问题。
比如,有多个类有相同的部分属性,把它们定义到父类中,恰好id(数据库主键)也在父类中,那么就会存在部分对象在比较时,它们并不相等,却因为lombok自动生成的equals(Object other) 和 hashCode()方法判定为相等,从而导致出错。
修复此问题的方法很简单:
使用@Getter @Setter @ToString代替@Data并且自定义equals(Object other) 和 hashCode()方法,比如有些类只需要判断主键id是否相等即足矣。
或者使用在使用@Data时同时加上@EqualsAndHashCode(callSuper=true)注解。
边栏推荐
- 阿里内网疯传的P8“顶级”分布式架构手册被我拿到了
- Intensive reading of the paper: deep reinforcement learning and intelligent transportation (I)
- Sword finger offer punch array and matrix
- Web开发人员的10个数据库优化最佳实践
- O & M - mélange de compétences
- 不知道如何提高视觉语言大模型?浙大与联汇研究院提出新型多维度评测框架
- Bisect module
- OPENGL学习(一)认识OPENGL和各种库
- Dimitra and ocean protocol interpret the secrets behind agricultural data
- [server data recovery] a data recovery case of RAID5 crash caused by hard disk offline during data synchronization of a hot spare disk of an IBM model
猜你喜欢

数字座舱新时代,头部企业博泰的洞见与预判

裁员吵架散摊子, 马斯克:我太难了;狠心开源了一个舆情获取项目;取特征工程跟调参一样简单了?!NeRF大佬直呼卷不动了;前沿论文 | ShowMeAI资讯日报

Xiamarin integrates braze to realize messaging and end-to-end notification

In the new era of digital cockpit, the insight and prediction of head enterprise Botai

2022年低压电工考试题模拟考试平台操作

What's the use of games| Game application value research case collection

利用 JMeter 压测上传和下载接口实战

迪文串口屏教程(2)

北航&信工所&美团提出LBDT,基于语言桥接的时空交互来进行准确指向性视频对象分割,性能SOTA!代码开源(CVPR 2022)...

Matplotlib绘图报错:“! LaTeX Error: File `type1cm.sty‘ not found.“ 解决办法
随机推荐
In the new era of digital cockpit, the insight and prediction of head enterprise Botai
牛啊!2小时复现顶会论文,他的秘诀是——
2022年安全员-A证考试题目及在线模拟考试
杰理之入耳检测功能【篇】
Do not know how to improve the visual language model? Zhejiang University and Lianhui Research Institute put forward a new multi-dimensional evaluation framework
Analysis of keepalive keeping alive mechanism of TCP protocol layer in IM development
Use excel2016's functions to generate random 16, 32, and 36 bit ID string contents
理财平台选择哪个安全可靠
2022安全员-C证特种作业证考试题库及答案
原厂直销MOS管 KNL42150 2.8A/1500V 适用光伏逆变器 可提供样品
因员工将密码设为“123456”,AMD 被盗 450Gb 数据?
Six capabilities of test and development
Acquisition data transmission mode of vibrating wire wireless acquisition instrument for engineering monitoring instrument
Huawei cloud stack opens its framework to the south to help ecological partners enter the cloud efficiently
爆肝万字,终于搞定这篇神经网络搭建全全全流程!学不会你来找我~
东软睿驰与联合电子达成战略合作,抢占国产化基础软件市场新风口
电脑网速检测在哪里可以找到
线性代数 笔记1
De la numérisation à l'exploitation et à la maintenance intelligentes: Quelles sont les valeurs et les défis?
Engineering monitoring vibrating wire wireless acquisition instrument external digital sensor access logic and data transmission