Hello,
In equalsVerifier 3.12.1 I ran into this new error
JPA Entity: direct reference to field xxx used in equals instead of getter getXXX
I have been unable to resolve it when the field is inherited from a parent class and the parent class equals and hashcode method use the getter. Below is a trivial example. Am I missing something?
Example:
@Entity
class Parent {
@Column(name = "text_content")
@Basic(fetch = FetchType.LAZY)
private String textContent;
public void setTextContent(String textContent) {
this.textContent = textContent
}
public String getTextContent() {
return this.textContent;
}
public int hashCode() {
return Objects.hash(getTextContent());
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Parent other = (Parent) obj;
return Objects.equals(getTextContent(), other.getTextContent());
}
}
@Entity
class Child extends Parent {
@Column(name = "child_content")
@Basic(fetch = FetchType.LAZY)
private String childContent;
public void setChildContent(String childContent) {
this.childContent = childContent
}
public String getChildContent() {
return this.childContent;
}
public int hashCode() {
return Objects.hash(super.hashCode(), getChildContent());
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
if(!super.equals(obj)) {
return false'
}
Child other = (Child) obj;
return Objects.equals(getChildContent(), other.getChildContent());
}
}
class ChildTest {
@Test
public void testEqualsAndHashcode() {
EqualsVerifier.forClass(Child.class).usingGetClass().suppress(Warning.NONFINAL_FIELDS).verify();
}
}