spring 当我使用查询查找时,boot jpa 正在更新我的数据库

spring boot jpa is updating my database when I am using find by query

这是所有 3 个实体的代码。 每当我做 repo.findByUserJavaIDAndCohortIdAndStatusInOrderByLastUpdatedDesc() 这也会更新我数据库中的 last_updated 列。

public abstract class BaseEntity     {
    
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(unique = true)
        protected Long id;
    
        @Column(unique = true, updatable = false, nullable = false)
        protected UUID uuid;
    
        @Builder.Default
        @Column(name = "is_deleted", nullable = false, columnDefinition = "bool default false")
        @JsonIgnore
        protected Boolean isDeleted=false;
    
        @Column(name = "created", nullable = false, updatable = false)
        @CreationTimestamp
        @JsonIgnore
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        protected Date created;
    
        @Column(name = "last_updated", nullable = false)
        @UpdateTimestamp
        @JsonIgnore
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        protected Date lastUpdated;
    
        public BaseEntity() {
            this.uuid = UUID.randomUUID();
            this.isDeleted = false;
        }
    }
    
    
    @Table(name = "task")
    public class Task extends BaseEntity {
    
        @NotNull
        @Column(nullable = false)
        private String title;
    
        @NotNull
        @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        private Source source;
    
        @NotNull
        @Column(name="remote_id", nullable = false)
        @JsonIgnore
        private String remoteId;
    
        @NotNull
        @Column(name = "cohort_id", nullable = false)
        private Long cohortId;
    
        @Builder.Default
        @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        @NotNull
        private TaskState state = TaskState.UPCOMING;
    
        @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        @NotNull
        private EventType type;
    
        @NotNull
        @Length(max = 5000)
        @Column(nullable = false, columnDefinition ="LONGTEXT" )
        private String agenda;
    
        @Type(type = "TaskExtraDataType")
        @Column(name = "extra_data", columnDefinition = "jsonb")
        private TaskExtraData extraData;
    
        @Column(name = "schedule_time")
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        protected Date scheduleTime;
    
        @Column(name = "start_time")
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        protected Date startTime;
    
        @Column(name = "end_time")
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
        protected Date endTime;
    
    }
    
    
    @Table(name = "user_task",
    public class UserTask extends BaseEntity {
    
        @ManyToOne
        @JoinColumn(name = "task_id")
        Task task;
    
        @NotNull
        @Column(name = "user_java_id", nullable = false)
        Long userJavaID;
    
        @NotNull
        @Column(name = "cohort_id", nullable = false)
        private Long cohortId;
    
        @Builder.Default
        @Column(nullable = false)
        @Enumerated(EnumType.STRING)
        UserTaskStatus status = UserTaskStatus.PENDING;
    
        @Type(type = "UserTaskExtraDataType")
        @Column(name = "extra_data", columnDefinition = "jsonb")
        private UserTaskExtraData userTaskExtraData;
    }

    public interface UserTaskRepository extends JpaRepository<UserTask, Long> {
        List<UserTask> findByUserJavaIDAndCohortIdAndStatusInOrderByLastUpdatedDesc(long userJavaId, long cohortId, List<UserTaskStatus> status);
    
    }

Hibernate 调用equals 方法来检查对象是否已经改变,在此之前它调用深复制方法来获取自定义对象的副本。所以我在我的额外 class 中添加了 equals 方法,它运行得很好。

public class UserTaskExtraData implements Serializable {
    private Integer timeSpentInMins;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UserTaskExtraData that = (UserTaskExtraData) o;
        return Objects.equals(timeSpentInMins, that.timeSpentInMins);
    }

    @Override
    public int hashCode() {
        return Objects.hash(timeSpentInMins);
    }
}
public class TaskExtraData implements Serializable {
    private String link;
    private String expertName;
    private Long expertId;
    private String expertImageUrl;
    private String eventImageUrl;
    private double duration;
    private String expertEmail;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TaskExtraData that = (TaskExtraData) o;
        return Double.compare(that.duration, duration) == 0 && Objects.equals(link, that.link) && Objects.equals(expertName, that.expertName) && Objects.equals(expertId, that.expertId) && Objects.equals(expertImageUrl, that.expertImageUrl) && Objects.equals(eventImageUrl, that.eventImageUrl) && Objects.equals(expertEmail, that.expertEmail);
    }

    @Override
    public int hashCode() {
        return Objects.hash(link, expertName, expertId, expertImageUrl, eventImageUrl, duration, expertEmail);
    }
}