- What language is this for? → Java
- Which rule? → java:S2097
Hi, we have a Hibernate entity class with equals and hashCode implemented as explained in https://jpa-buddy.com/blog/hopefully-the-final-article-about-equals-and-hashcode-for-jpa-entities-with-db-generated-ids/ and now we get a new reliability issue because of java:S2097 stating that there is no type test inside the equals method.
The code is as follows:
@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy oHibernateProxy ? oHibernateProxy.getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy thisHibernateProxy ? thisHibernateProxy.getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
MyEntity myEntity = (MyEntity) o;
return getId() != null && Objects.equals(getId(), myEntity.getId());
}
the class containing this method is called MyEntity and there is a type test (if (thisEffectiveClass != oEffectiveClass))
We’re using SonarQube Server Developer Edition v2025.4.2