首页 > 解决方案 > 将 BaseEntity 用于 JPA 实体时,Sonar 中的“等于错误”

问题描述

我们正在为 JPA 实体使用 BaseEntity 类,它实现了如下的 equals 方法。

@Override
public boolean equals(Object object) {
    if (this == object) {
        return true;
    }

    if (object == null) {
        return false;
    }

    if (getClass() != object.getClass()) {
        return false;
    }

    BaseEntity other = (BaseEntity) object;
    if (this.getId() == null && other.getId() == null) {
        return false;
    }

    if (this.getId() != other.getId() && (this.getId() == null || !this.id.equals(other.id))) {
        return false;
    }

    return true;
}

对于扩展 BaseEntity Sonar 的所有类,显示代码异味问题。

Override the "equals" method in this class.

在此处输入图像描述

我们应该如何处理?

标签: javajpasonarqube

解决方案


推荐阅读