Skip to content

Commit

Permalink
Adds Warning.JPA_GETTER
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jul 6, 2023
1 parent 41c219c commit 0fdbd43
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ public enum Warning {
*/
SURROGATE_OR_BUSINESS_KEY,

/**
* Disables the check that collection fields in JPA, or @Basic fields marked with
* FetchType.LAZY, should be accessed through their getter methods in {@code equals} and
* {@code hashCode} methods.
*
* <p>Normally, it is necessary to go through the getter for these fields, because their
* content may not be materialized in some instances. Calling the getter will materialize them,
* but referencing the field directly will not. This can lead to situations where the
* {@code equals} method of objects that should be equal to each other returns false, because
* one instance has the content materialized and the other does not.
*/
JPA_GETTER,

/**
* Disables the check that transient fields not be part of the {@code equals} contract.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ public void check() {
}

AnnotationCache cache = config.getAnnotationCache();
if (cache.hasClassAnnotation(config.getType(), SupportedAnnotations.ENTITY)) {
if (
cache.hasClassAnnotation(config.getType(), SupportedAnnotations.ENTITY) &&
!config.getWarningsToSuppress().contains(Warning.JPA_GETTER)
) {
inspector.check(jpaLazyGetterFieldCheck);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,49 @@ public void basicGetterNotUsed_givenCorrespondingFieldIgnored() {
@Test
public void basicGetterUsed_givenAnnotationIsOnGetter() {
getterNotUsed(IncorrectBasicJpaLazyGetterContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectBasicJpaLazyGetterContainer.class);
}

@Test
public void basicGetterNotUsedInHashCode() {
getterNotUsed(IncorrectBasicJpaLazyFieldContainerHashCode.class, "hashCode");
getterNotUsed_warningSuppressed(IncorrectBasicJpaLazyFieldContainerHashCode.class);
}

@Test
public void basicGetterNotUsed() {
getterNotUsed(IncorrectBasicJpaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectBasicJpaLazyFieldContainer.class);
}

@Test
public void oneToOneGetterNotUsed() {
getterNotUsed(IncorrectOneToOneJpaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectOneToOneJpaLazyFieldContainer.class);
}

@Test
public void oneToManyGetterNotUsed() {
getterNotUsed(IncorrectOneToManyJpaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectOneToManyJpaLazyFieldContainer.class);
}

@Test
public void manyToOneGetterNotUsed() {
getterNotUsed(IncorrectManyToOneJpaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectManyToOneJpaLazyFieldContainer.class);
}

@Test
public void manyToManyGetterNotUsed() {
getterNotUsed(IncorrectManyToManyJpaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectManyToManyJpaLazyFieldContainer.class);
}

@Test
public void elementCollectionGetterNotUsed() {
getterNotUsed(IncorrectElementCollectionJpaLazyFieldContainer.class, "equals");
getterNotUsed_warningSuppressed(IncorrectElementCollectionJpaLazyFieldContainer.class);
}

@Test
Expand Down Expand Up @@ -131,6 +139,13 @@ private void getterNotUsed(Class<?> type, String method) {
.assertMessageContains("JPA Entity", method, "direct reference");
}

private void getterNotUsed_warningSuppressed(Class<?> type) {
EqualsVerifier
.forClass(type)
.suppress(Warning.JPA_GETTER, Warning.NONFINAL_FIELDS)
.verify();
}

@Entity
static class CorrectJpaLazyFieldContainer {

Expand Down

0 comments on commit 0fdbd43

Please sign in to comment.