refactor(jpa): apply Hibernate-safe equals and hashCode implementations (#1089)

This commit is contained in:
brios
2026-05-09 10:51:35 +02:00
committed by GitHub
parent 319b3a572b
commit 76c6ca7394
8 changed files with 69 additions and 34 deletions
@@ -2,6 +2,7 @@ package org.booklore.model.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.BatchSize;
import java.util.HashSet; import java.util.HashSet;
@@ -48,14 +49,15 @@ public class AuthorEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>(); private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override @Override
public boolean equals(Object o) { public final boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof AuthorEntity that)) return false; if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
return id != null && Objects.equals(id, that.id); AuthorEntity that = (AuthorEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
} }
@Override @Override
public int hashCode() { public final int hashCode() {
return getClass().hashCode(); return Hibernate.getClass(this).hashCode();
} }
} }
@@ -5,6 +5,7 @@ import lombok.*;
import org.booklore.convertor.BookRecommendationIdsListConverter; import org.booklore.convertor.BookRecommendationIdsListConverter;
import org.booklore.model.dto.BookRecommendationLite; import org.booklore.model.dto.BookRecommendationLite;
import org.booklore.model.enums.BookFileType; import org.booklore.model.enums.BookFileType;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.LazyGroup; import org.hibernate.annotations.LazyGroup;
@@ -14,6 +15,7 @@ import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
@Entity @Entity
@@ -131,4 +133,17 @@ public class BookEntity {
public boolean hasFiles() { public boolean hasFiles() {
return bookFiles != null && !bookFiles.isEmpty(); return bookFiles != null && !bookFiles.isEmpty();
} }
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null || !Hibernate.getClass(this).equals(Hibernate.getClass(o))) return false;
BookEntity that = (BookEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
} }
@@ -65,11 +65,7 @@ public class BookLoreUserEntity {
@BatchSize(size = 20) @BatchSize(size = 20)
@ManyToMany(fetch = FetchType.LAZY) @ManyToMany(fetch = FetchType.LAZY)
@JoinTable( @JoinTable(name = "user_library_mapping", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "library_id"))
name = "user_library_mapping",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "library_id")
)
@Builder.Default @Builder.Default
private Set<LibraryEntity> libraries = new HashSet<>(); private Set<LibraryEntity> libraries = new HashSet<>();
@@ -97,15 +93,15 @@ public class BookLoreUserEntity {
} }
@Override @Override
public boolean equals(Object o) { public final boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false; if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
BookLoreUserEntity that = (BookLoreUserEntity) o; BookLoreUserEntity that = (BookLoreUserEntity) o;
return id != null && Objects.equals(id, that.id); return getId() != null && Objects.equals(getId(), that.getId());
} }
@Override @Override
public int hashCode() { public final int hashCode() {
return Hibernate.getClass(this).hashCode(); return Hibernate.getClass(this).hashCode();
} }
} }
@@ -2,6 +2,7 @@ package org.booklore.model.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.BatchSize;
import java.util.HashSet; import java.util.HashSet;
@@ -30,15 +31,16 @@ public class CategoryEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>(); private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override @Override
public boolean equals(Object o) { public final boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof CategoryEntity that)) return false; if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
return id != null && Objects.equals(id, that.id); CategoryEntity that = (CategoryEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
} }
@Override @Override
public int hashCode() { public final int hashCode() {
return getClass().hashCode(); return Hibernate.getClass(this).hashCode();
} }
} }
@@ -9,6 +9,7 @@ import org.booklore.model.enums.LibraryOrganizationMode;
import org.booklore.model.enums.MetadataSource; import org.booklore.model.enums.MetadataSource;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.BatchSize;
import java.util.*; import java.util.*;
@@ -75,15 +76,16 @@ public class LibraryEntity {
private MetadataSource metadataSource = MetadataSource.EMBEDDED; private MetadataSource metadataSource = MetadataSource.EMBEDDED;
@Override @Override
public boolean equals(Object o) { public final boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof LibraryEntity that)) return false; if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
return id != null && Objects.equals(id, that.id); LibraryEntity that = (LibraryEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
} }
@Override @Override
public int hashCode() { public final int hashCode() {
return getClass().hashCode(); return Hibernate.getClass(this).hashCode();
} }
} }
@@ -2,6 +2,7 @@ package org.booklore.model.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.BatchSize;
import java.util.HashSet; import java.util.HashSet;
@@ -30,15 +31,16 @@ public class MoodEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>(); private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override @Override
public boolean equals(Object o) { public final boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof MoodEntity that)) return false; if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
return id != null && Objects.equals(id, that.id); MoodEntity that = (MoodEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
} }
@Override @Override
public int hashCode() { public final int hashCode() {
return getClass().hashCode(); return Hibernate.getClass(this).hashCode();
} }
} }
@@ -5,10 +5,12 @@ import lombok.*;
import org.booklore.convertor.SortConverter; import org.booklore.convertor.SortConverter;
import org.booklore.model.dto.Sort; import org.booklore.model.dto.Sort;
import org.booklore.model.enums.IconType; import org.booklore.model.enums.IconType;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Formula; import org.hibernate.annotations.Formula;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
@Getter @Getter
@@ -16,7 +18,6 @@ import java.util.Set;
@Builder @Builder
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@EqualsAndHashCode(of = "id")
@Entity @Entity
@Table(name = "shelf") @Table(name = "shelf")
public class ShelfEntity { public class ShelfEntity {
@@ -57,4 +58,17 @@ public class ShelfEntity {
) )
@Builder.Default @Builder.Default
private Set<BookEntity> bookEntities = new HashSet<>(); private Set<BookEntity> bookEntities = new HashSet<>();
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
ShelfEntity that = (ShelfEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
} }
@@ -2,6 +2,7 @@ package org.booklore.model.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize; import org.hibernate.annotations.BatchSize;
import java.util.HashSet; import java.util.HashSet;
@@ -30,14 +31,15 @@ public class TagEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>(); private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override @Override
public boolean equals(Object o) { public final boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;
if (!(o instanceof TagEntity that)) return false; if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
return id != null && Objects.equals(id, that.id); TagEntity that = (TagEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
} }
@Override @Override
public int hashCode() { public final int hashCode() {
return getClass().hashCode(); return Hibernate.getClass(this).hashCode();
} }
} }