当前位置:网站首页>Lombok @Data导致的hashCode的问题
Lombok @Data导致的hashCode的问题
2022-07-13 17:28:00 【三横兰】
目录
背景
Entity
一个User包含多个AbstractUser。在jpa查询中因为用到left join fetch,导致查询结果AbstractUser重复,而又没有办法在第二层利用distinct关键字去重AbstractUser,所以利用SET来过滤。但是利用Lombok的@DATA注解导致错误。
User
@Entity
@Table(name = "user")
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class User{
@Id
@GeneratedValue
@Column(name = "uuid")
private UUID id;
@Column(name = "name", nullable = false)
private String name;
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private Set<AbstractUser> abstractUsers;
}
AbstractUser
@Entity
@Table(name = "abstract_user")
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class AbstractUser implements Serializable{
/** * */
/** The unique identifier */
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = "uuid", unique = true, nullable = false)
@Id
private String id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@JsonIgnore
private User user;
}
利用下面的JPA语句查询的时候报错了
@Query("select distinct u from User u left join fetch u.abstractUsers as au "
+ "where (:username is null or lower(u.name)=lower(:username)) "
+ "and (:abstractUserId is null or au.id=:abstractUserId ")
List<User> findUsers(@Param("username") String username, @Param("abstractUserId") String abstractUserId);
问题
java.lang.NullPointerException: null
at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:789) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:58) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.event.spi.InitializeCollectionEvent.(InitializeCollectionEvent.java:22) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2244) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:580) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:262) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:576) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:147) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.collection.internal.PersistentSet.hashCode(PersistentSet.java:458) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at com.test.AbstractUser.hashCode(AbstractUser.java:43) ~[classes/:na]
at java.util.HashMap.hash(Unknown Source) ~[na:1.8.0_151]
at java.util.HashMap.put(Unknown Source) ~[na:1.8.0_151]
at java.util.HashSet.add(Unknown Source) ~[na:1.8.0_151]
at java.util.AbstractCollection.addAll(Unknown Source) ~[na:1.8.0_151]
原因
@Data相当于@Getter@Setter@RequiredArgsConstructor@ToString ``@EqualsAndHashCode这5个注解的合集。
当使用@Data注解时,则有了@EqualsAndHashCode注解,那么就会在此类中存在equals(Object other) 和 hashCode()方法,且不会使用父类的属性,这就导致了可能的问题。
比如,有多个类有相同的部分属性,把它们定义到父类中,恰好id(数据库主键)也在父类中,那么就会存在部分对象在比较时,它们并不相等,却因为lombok自动生成的equals(Object other) 和 hashCode()方法判定为相等,从而导致出错。
hashSet过滤去重的时候使用hashCode方式生成的hashcode来判别对象是否重复。lombok的@Data注解生成的hashCode()导致出错。
解决办法
- 使用
@Getter`` @Setter`` @ToString代替@Data并且自定义equals(Object other) 和 hashCode()方法,比如有些类只需要判断主键id是否相等即足矣。 - 或者在使用@Data时同时加上
@EqualsAndHashCode(callSuper=true)注解。本文情况因为没有父类,所以这个方法不适用,如果有用到父类的话可以使用。 - 如果没有父类的话,使用
@DATA同时加上@EqualsAndHashCode(of = {"id"})。指定仅使用id属性,如果有多个可以确定对象唯一的属性的话也可以继续在{}中添加。 - 使用
@DATA同时加上@EqualsAndHashCode(onlyExplicitlyIncluded = true),然后在需要执行的属性加上@EqualsAndHashCode.Include,如:
/** The unique identifier */
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
@Column(name = UNIQUE_ID_COLUMN_NAME, unique = true, nullable = false)
@Id
@EqualsAndHashCode.Include
private String id;
参考:
https://stackoverflow.com/questions/33893122/jpa-hibernate-find-throws-nullpointerexception.
https://blog.csdn.net/zhanlanmg/article/details/50392266.
http://www.javabyexamples.com/delombok-equalsandhashcode/
边栏推荐
- build_opencv.sh
- [explore why string class is an immutable type: String class imitation]
- ECCV2022 | 腾讯优图29篇论文入选,含人脸安全、图像分割、目标检测等多个研究方向
- How to view cookies in Internet Explorer
- 视频孪生:智慧园区信息化升级的抓手
- LeNet5、AlexNet、VGGNet、ResNet
- fenxi
- [regression prediction LSTM] LSTM implementation based on attention mechanism regression prediction of time series with matlab code
- [底层原理]Socket 究竟是什么? 为啥网络离不开 Socket?
- 电脑文件无法删除怎么办?
猜你喜欢

一个描述机器学习对温室气体排放影响的系统框架

一文理解分布式开发中的服务治理

Mysql-MVCC

How to write effective interface tests?

看看谷歌如何在目标检测任务使用预训练权值 | CVPR 2022

Ardunio——触摸传感器和灯光的互动——根据触摸屏判定是否亮灯

软件架构图表与模式介绍

opencv的实战学习之信用卡识别(四)
![[route optimization] HWSN energy-saving clustering protocol based on biogeography optimization with matlab code](/img/56/c3e4ada26101d243a5c1aa840f56ba.png)
[route optimization] HWSN energy-saving clustering protocol based on biogeography optimization with matlab code

Common tools and test methods for interface testing
随机推荐
这几款手机安全浏览器,好用不止一点点
Modeling technology of complex urban drainage system based on SWMM and self-developed urban waterlogging one-dimensional and two-dimensional coupling software and its practice in urban waterlogging, s
ObjectARX select entities to create block references
任务管理器显示不全怎么办?
What if the system always breaks down? Maybe you should learn stability construction!
一文理解分布式开发中的服务治理
LeNet5、AlexNet、VGGNet、ResNet
接口测试常用工具及测试方法
caffe fine tune
How to shield yandexbot
Autodesk Desktop Licensing Service出现错误1067处理办法
Some interview questions encountered recently -- WPF, C #, database
如何关闭卡死的程序进程?
C3D的使用
Class notes (3) example (2) -567 Beanfeast
[底层原理]Socket 究竟是什么? 为啥网络离不开 Socket?
番茄定时调光台灯触摸芯片-DLT8T10S-杰力科创
See you in Chengdu, starrocks! How can enterprises create a new paradigm of rapid and unified data analysis to help businesses upgrade in an all-round way
A systematic framework for describing the impact of machine learning on greenhouse gas emissions
分布式单体的六大病症