Skip to content

Commit

Permalink
Requires getters for JPA collection fields even if they're not lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Jul 6, 2023
1 parent 3f74109 commit 71ac78c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,21 @@ public void postProcess(Set<Class<?>> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ public void gettersAreUsed() {
@Test
public void basicGetterNotUsed_givenEagerLoading() {
EqualsVerifier
.forClass(IncorrectBasicJpaEagerFieldContainer.class)
.forClass(CorrectBasicJpaEagerFieldContainer.class)
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
public void basicGetterNotUsed_givenCorrespondingFieldIgnored() {
EqualsVerifier
.forClass(IncorrectBasicJpaIgnoredLazyFieldContainer.class)
.forClass(CorrectBasicJpaIgnoredLazyFieldContainer.class)
.withIgnoredFields("basic")
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Test
public void basicGetterUsed_givenAnnotationIsOnGetter() {
getterNotUsed(CorrectBasicJpaLazyGetterContainer.class, "equals");
getterNotUsed(IncorrectBasicJpaLazyGetterContainer.class, "equals");
}

@Test
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -206,7 +206,7 @@ public int hashCode() {
}

@Entity
static class IncorrectBasicJpaEagerFieldContainer {
static class CorrectBasicJpaEagerFieldContainer {

@Basic
private String basic;
Expand All @@ -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);
}

Expand All @@ -231,7 +231,7 @@ public int hashCode() {
}

@Entity
static class IncorrectBasicJpaIgnoredLazyFieldContainer {
static class CorrectBasicJpaIgnoredLazyFieldContainer {

private String somethingElse;

Expand All @@ -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);
}

Expand All @@ -259,7 +259,7 @@ public int hashCode() {
}

@Entity
static class CorrectBasicJpaLazyGetterContainer {
static class IncorrectBasicJpaLazyGetterContainer {

private String basic;

Expand All @@ -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);
}

Expand Down Expand Up @@ -337,7 +337,7 @@ public int hashCode() {
@Entity
static class IncorrectOneToOneJpaLazyFieldContainer {

@OneToOne(fetch = FetchType.LAZY)
@OneToOne
private String oneToOne;

public String getOneToOne() {
Expand All @@ -363,7 +363,7 @@ public int hashCode() {
@Entity
static class IncorrectOneToManyJpaLazyFieldContainer {

@OneToMany(fetch = FetchType.LAZY)
@OneToMany
private String oneToMany;

public String getOneToMany() {
Expand All @@ -389,7 +389,7 @@ public int hashCode() {
@Entity
static class IncorrectManyToOneJpaLazyFieldContainer {

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne
private String manyToOne;

public String getManyToOne() {
Expand All @@ -415,7 +415,7 @@ public int hashCode() {
@Entity
static class IncorrectManyToManyJpaLazyFieldContainer {

@ManyToMany(fetch = FetchType.LAZY)
@ManyToMany
private String manyToMany;

public String getManyToMany() {
Expand All @@ -441,7 +441,7 @@ public int hashCode() {
@Entity
static class IncorrectElementCollectionJpaLazyFieldContainer {

@ElementCollection(fetch = FetchType.LAZY)
@ElementCollection
private String elementCollection;

public String getElementCollection() {
Expand Down

0 comments on commit 71ac78c

Please sign in to comment.