From 41c219c7a7365f15ed42cf80402816cd94bad7aa Mon Sep 17 00:00:00 2001 From: Jan Ouwens Date: Thu, 6 Jul 2023 12:21:10 +0200 Subject: [PATCH] Requires getters for JPA collection fields even if they're not lazy --- .../fieldchecks/JpaLazyGetterFieldCheck.java | 15 +++++-- .../annotations/SupportedAnnotations.java | 8 ++-- .../extra_features/JpaLazyEntityTest.java | 44 +++++++++---------- 3 files changed, 37 insertions(+), 30 deletions(-) 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 d4ad11fd4..0ae094cad 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 @@ -73,10 +73,17 @@ public void execute( } private boolean fieldIsLazy(FieldAccessor fieldAccessor) { - return annotationCache.hasFieldAnnotation( - type, - fieldAccessor.getFieldName(), - SupportedAnnotations.LAZY_FIELD + return ( + annotationCache.hasFieldAnnotation( + type, + fieldAccessor.getFieldName(), + SupportedAnnotations.JPA_COLLECTION_FIELD + ) || + annotationCache.hasFieldAnnotation( + type, + fieldAccessor.getFieldName(), + SupportedAnnotations.JPA_LAZY_FIELD + ) ); } diff --git a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/annotations/SupportedAnnotations.java b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/annotations/SupportedAnnotations.java index dcf8a02b7..8add3903d 100644 --- a/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/annotations/SupportedAnnotations.java +++ b/equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/reflection/annotations/SupportedAnnotations.java @@ -194,21 +194,21 @@ public void postProcess(Set> types, AnnotationCache annotationCache) { } }, - LAZY_FIELD( + JPA_COLLECTION_FIELD( true, - "javax.persistence.Basic", "javax.persistence.OneToOne", "javax.persistence.OneToMany", "javax.persistence.ManyToOne", "javax.persistence.ManyToMany", "javax.persistence.ElementCollection", - "jakarta.persistence.Basic", "jakarta.persistence.OneToOne", "jakarta.persistence.OneToMany", "jakarta.persistence.ManyToOne", "jakarta.persistence.ManyToMany", "jakarta.persistence.ElementCollection" - ) { + ), + + JPA_LAZY_FIELD(true, "javax.persistence.Basic", "jakarta.persistence.Basic") { @Override public boolean validate( AnnotationProperties properties, 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 4d442fa1e..a27451e51 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 @@ -30,7 +30,7 @@ public void gettersAreUsed() { @Test public void basicGetterNotUsed_givenEagerLoading() { EqualsVerifier - .forClass(IncorrectBasicJpaEagerFieldContainer.class) + .forClass(CorrectBasicJpaEagerFieldContainer.class) .suppress(Warning.NONFINAL_FIELDS) .verify(); } @@ -38,7 +38,7 @@ public void basicGetterNotUsed_givenEagerLoading() { @Test public void basicGetterNotUsed_givenCorrespondingFieldIgnored() { EqualsVerifier - .forClass(IncorrectBasicJpaIgnoredLazyFieldContainer.class) + .forClass(CorrectBasicJpaIgnoredLazyFieldContainer.class) .withIgnoredFields("basic") .suppress(Warning.NONFINAL_FIELDS) .verify(); @@ -46,7 +46,7 @@ public void basicGetterNotUsed_givenCorrespondingFieldIgnored() { @Test public void basicGetterUsed_givenAnnotationIsOnGetter() { - getterNotUsed(CorrectBasicJpaLazyGetterContainer.class, "equals"); + getterNotUsed(IncorrectBasicJpaLazyGetterContainer.class, "equals"); } @Test @@ -137,16 +137,16 @@ static class CorrectJpaLazyFieldContainer { @Basic(fetch = FetchType.LAZY) private String basic; - @OneToOne(fetch = FetchType.LAZY) + @OneToOne private String oneToOne; - @OneToMany(fetch = FetchType.LAZY) + @OneToMany private String oneToMany; - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne private String manyToOne; - @ManyToMany(fetch = FetchType.LAZY) + @ManyToMany private String manyToMany; @ElementCollection(fetch = FetchType.LAZY) @@ -206,7 +206,7 @@ public int hashCode() { } @Entity - static class IncorrectBasicJpaEagerFieldContainer { + static class CorrectBasicJpaEagerFieldContainer { @Basic private String basic; @@ -217,10 +217,10 @@ public String getBasic() { @Override public boolean equals(Object obj) { - if (!(obj instanceof IncorrectBasicJpaEagerFieldContainer)) { + if (!(obj instanceof CorrectBasicJpaEagerFieldContainer)) { return false; } - IncorrectBasicJpaEagerFieldContainer other = (IncorrectBasicJpaEagerFieldContainer) obj; + CorrectBasicJpaEagerFieldContainer other = (CorrectBasicJpaEagerFieldContainer) obj; return Objects.equals(basic, other.basic); } @@ -231,7 +231,7 @@ public int hashCode() { } @Entity - static class IncorrectBasicJpaIgnoredLazyFieldContainer { + static class CorrectBasicJpaIgnoredLazyFieldContainer { private String somethingElse; @@ -244,11 +244,11 @@ public String getBasic() { @Override public boolean equals(Object obj) { - if (!(obj instanceof IncorrectBasicJpaIgnoredLazyFieldContainer)) { + if (!(obj instanceof CorrectBasicJpaIgnoredLazyFieldContainer)) { return false; } - IncorrectBasicJpaIgnoredLazyFieldContainer other = - (IncorrectBasicJpaIgnoredLazyFieldContainer) obj; + CorrectBasicJpaIgnoredLazyFieldContainer other = + (CorrectBasicJpaIgnoredLazyFieldContainer) obj; return Objects.equals(somethingElse, other.somethingElse); } @@ -259,7 +259,7 @@ public int hashCode() { } @Entity - static class CorrectBasicJpaLazyGetterContainer { + static class IncorrectBasicJpaLazyGetterContainer { private String basic; @@ -270,10 +270,10 @@ public String getBasic() { @Override public boolean equals(Object obj) { - if (!(obj instanceof CorrectBasicJpaLazyGetterContainer)) { + if (!(obj instanceof IncorrectBasicJpaLazyGetterContainer)) { return false; } - CorrectBasicJpaLazyGetterContainer other = (CorrectBasicJpaLazyGetterContainer) obj; + IncorrectBasicJpaLazyGetterContainer other = (IncorrectBasicJpaLazyGetterContainer) obj; return Objects.equals(basic, other.basic); } @@ -337,7 +337,7 @@ public int hashCode() { @Entity static class IncorrectOneToOneJpaLazyFieldContainer { - @OneToOne(fetch = FetchType.LAZY) + @OneToOne private String oneToOne; public String getOneToOne() { @@ -363,7 +363,7 @@ public int hashCode() { @Entity static class IncorrectOneToManyJpaLazyFieldContainer { - @OneToMany(fetch = FetchType.LAZY) + @OneToMany private String oneToMany; public String getOneToMany() { @@ -389,7 +389,7 @@ public int hashCode() { @Entity static class IncorrectManyToOneJpaLazyFieldContainer { - @ManyToOne(fetch = FetchType.LAZY) + @ManyToOne private String manyToOne; public String getManyToOne() { @@ -415,7 +415,7 @@ public int hashCode() { @Entity static class IncorrectManyToManyJpaLazyFieldContainer { - @ManyToMany(fetch = FetchType.LAZY) + @ManyToMany private String manyToMany; public String getManyToMany() { @@ -441,7 +441,7 @@ public int hashCode() { @Entity static class IncorrectElementCollectionJpaLazyFieldContainer { - @ElementCollection(fetch = FetchType.LAZY) + @ElementCollection private String elementCollection; public String getElementCollection() {