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 lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;
import java.util.HashSet;
@@ -48,14 +49,15 @@ public class AuthorEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof AuthorEntity that)) return false;
return id != null && Objects.equals(id, that.id);
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
AuthorEntity that = (AuthorEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return getClass().hashCode();
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
}
@@ -5,6 +5,7 @@ import lombok.*;
import org.booklore.convertor.BookRecommendationIdsListConverter;
import org.booklore.model.dto.BookRecommendationLite;
import org.booklore.model.enums.BookFileType;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.LazyGroup;
@@ -14,6 +15,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@Entity
@@ -131,4 +133,17 @@ public class BookEntity {
public boolean hasFiles() {
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)
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "user_library_mapping",
joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "library_id")
)
@JoinTable(name = "user_library_mapping", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "library_id"))
@Builder.Default
private Set<LibraryEntity> libraries = new HashSet<>();
@@ -97,15 +93,15 @@ public class BookLoreUserEntity {
}
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
BookLoreUserEntity that = (BookLoreUserEntity) o;
return id != null && Objects.equals(id, that.id);
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
}
@@ -2,6 +2,7 @@ package org.booklore.model.entity;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;
import java.util.HashSet;
@@ -30,15 +31,16 @@ public class CategoryEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof CategoryEntity that)) return false;
return id != null && Objects.equals(id, that.id);
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
CategoryEntity that = (CategoryEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return getClass().hashCode();
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
}
@@ -9,6 +9,7 @@ import org.booklore.model.enums.LibraryOrganizationMode;
import org.booklore.model.enums.MetadataSource;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;
import java.util.*;
@@ -75,15 +76,16 @@ public class LibraryEntity {
private MetadataSource metadataSource = MetadataSource.EMBEDDED;
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof LibraryEntity that)) return false;
return id != null && Objects.equals(id, that.id);
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
LibraryEntity that = (LibraryEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return getClass().hashCode();
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
}
@@ -2,6 +2,7 @@ package org.booklore.model.entity;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;
import java.util.HashSet;
@@ -30,15 +31,16 @@ public class MoodEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof MoodEntity that)) return false;
return id != null && Objects.equals(id, that.id);
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
MoodEntity that = (MoodEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return getClass().hashCode();
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
}
@@ -5,10 +5,12 @@ import lombok.*;
import org.booklore.convertor.SortConverter;
import org.booklore.model.dto.Sort;
import org.booklore.model.enums.IconType;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Formula;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
@Getter
@@ -16,7 +18,6 @@ import java.util.Set;
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = "id")
@Entity
@Table(name = "shelf")
public class ShelfEntity {
@@ -57,4 +58,17 @@ public class ShelfEntity {
)
@Builder.Default
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 lombok.*;
import org.hibernate.Hibernate;
import org.hibernate.annotations.BatchSize;
import java.util.HashSet;
@@ -30,14 +31,15 @@ public class TagEntity {
private Set<BookMetadataEntity> bookMetadataEntityList = new HashSet<>();
@Override
public boolean equals(Object o) {
public final boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof TagEntity that)) return false;
return id != null && Objects.equals(id, that.id);
if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) return false;
TagEntity that = (TagEntity) o;
return getId() != null && Objects.equals(getId(), that.getId());
}
@Override
public int hashCode() {
return getClass().hashCode();
public final int hashCode() {
return Hibernate.getClass(this).hashCode();
}
}