From 346c69b91cb15fca9f274e8c23f1e1bb18b56265 Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Fri, 1 Mar 2024 10:06:35 +0100 Subject: [PATCH] Allows final entities with @GeneratedValue --- CHANGELOG.md | 4 +++ .../fieldchecks/JpaLazyGetterFieldCheck.java | 4 ++- .../extra_features/JakartaLazyEntityTest.java | 30 +++++++++++++++++++ .../extra_features/JpaLazyEntityTest.java | 30 +++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 062e1033a..010004b64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Final entities with @GeneratedValue could not be verified. ([Issue 929](https://github.com/jqno/equalsverifier/issues/929)) + ## [3.15.7] - 2024-02-23 ### Fixed diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java index 8aaeccdbe..5ecd18ca9 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/checkers/fieldchecks/JpaLazyGetterFieldCheck.java @@ -4,6 +4,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import static nl.jqno.equalsverifier.internal.util.Assert.assertTrue; +import java.lang.reflect.Modifier; import java.util.function.Function; import nl.jqno.equalsverifier.Warning; import nl.jqno.equalsverifier.internal.exceptions.EqualsVerifierInternalBugException; @@ -49,7 +50,8 @@ public void execute( if ( !fieldIsUsed(referenceAccessor, copyAccessor, fieldAccessor, true) || - !fieldIsLazy(fieldAccessor) + !fieldIsLazy(fieldAccessor) || + Modifier.isFinal(type.getModifiers()) ) { return; } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JakartaLazyEntityTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JakartaLazyEntityTest.java index 35c7f78df..a9f29ce92 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JakartaLazyEntityTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JakartaLazyEntityTest.java @@ -176,6 +176,11 @@ public void gettersAreUsedAndProtected() { EqualsVerifier.forClass(ProtectedJakartaLazyFieldContainer.class).verify(); } + @Test + public void finalEntitiesArentLazy() { + EqualsVerifier.forClass(FinalEntity.class).verify(); + } + private void getterNotUsed(Class type, String method, Warning... additionalWarnings) { ExpectedException .when(() -> EqualsVerifier.forClass(type).suppress(additionalWarnings).verify()) @@ -718,4 +723,29 @@ public int hashCode() { return Objects.hash(getBasic()); } } + + @Entity + static final class FinalEntity { + + @GeneratedValue // which is considered lazy + private int id; + + public int getId() { + return id; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof FinalEntity)) { + return false; + } + FinalEntity other = (FinalEntity) obj; + return Objects.equals(getId(), other.getId()); + } + + @Override + public int hashCode() { + return Objects.hash(getId()); + } + } } diff --git a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JpaLazyEntityTest.java b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JpaLazyEntityTest.java index afb29534b..4c926eae1 100644 --- a/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JpaLazyEntityTest.java +++ b/equalsverifier-core/src/test/java/nl/jqno/equalsverifier/integration/extra_features/JpaLazyEntityTest.java @@ -166,6 +166,11 @@ public void gettersAreUsedAndProtected() { EqualsVerifier.forClass(ProtectedJpaLazyFieldContainer.class).verify(); } + @Test + public void finalEntitiesArentLazy() { + EqualsVerifier.forClass(FinalEntity.class).verify(); + } + private void getterNotUsed(Class type, String method, Warning... additionalWarnings) { ExpectedException .when(() -> EqualsVerifier.forClass(type).suppress(additionalWarnings).verify()) @@ -705,4 +710,29 @@ public int hashCode() { return Objects.hash(getBasic()); } } + + @Entity + static final class FinalEntity { + + @GeneratedValue // which is considered lazy + private int id; + + public int getId() { + return id; + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof FinalEntity)) { + return false; + } + FinalEntity other = (FinalEntity) obj; + return Objects.equals(getId(), other.getId()); + } + + @Override + public int hashCode() { + return Objects.hash(getId()); + } + } }