龙目岛@EqualsAndHashCode和Jhipster TestUtil.equalsVerifier

问题描述

我正在将Jhipster和Lombok用于我的Java项目。

删除了Jhipster生成的实体对象equals()和hashcode()方法,并替换为lombok @EqualsAndHashCode

最初由Jhipster生成

@Entity
@Table(name = "category")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Category implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Size(max = 100)
    @Column(name = "name",length = 100)
    private String name;

    // jhipster-needle-entity-add-field - JHipster will add fields here
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public Category name(String name) {
        this.name = name;
        return this;
    }

    public void setName(String name) {
        this.name = name;
    }
    // jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof Category)) {
            return false;
        }
        return id != null && id.equals(((Category) o).id);
    }

    @Override
    public int hashCode() {
        return 31;
    }

    // prettier-ignore
    @Override
    public String toString() {
        return "Category{" +
            "id=" + getId() +
            ",name='" + getName() + "'" +
            "}";
    }
}

修改为使用龙目岛


@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@Table(name = "category")
public class Category extends AbstractAuditingEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Size(max = 100)
    @Column(name = "name",length = 100)
    private String name;

}

问题是下面的Jhipster生成的测试失败。在测试的最后两行

    /**
     * Verifies the equals/hashcode contract on the domain object.
     */
    public static <T> void equalsverifier(Class<T> clazz) throws Exception {
        T domainObject1 = clazz.getConstructor().newInstance();
        assertthat(domainObject1.toString()).isNotNull();
        assertthat(domainObject1).isEqualTo(domainObject1);
        assertthat(domainObject1.hashCode()).isEqualTo(domainObject1.hashCode());
        // Test with an instance of another class
        Object testOtherObject = new Object();
        assertthat(domainObject1).isNotEqualTo(testOtherObject);
        assertthat(domainObject1).isNotEqualTo(null);
        // Test with an instance of the same class
        T domainObject2 = clazz.getConstructor().newInstance();
        assertthat(domainObject1).isNotEqualTo(domainObject2);  //this fails
        // HashCodes are equals because the objects are not persisted yet
        assertthat(domainObject1.hashCode()).isEqualTo(domainObject2.hashCode()); //this also fails
    }

我可以知道考试的目的吗?

每个clazz.getConstructor().newInstance();对象不应该彼此相等吗?对象的内容是相同的。为什么不呢?

每个clazz.getConstructor().newInstance();对象应该具有相同的hashCode吗?如果是这样,为什么lombok @EqualsAndHashCode给出了不同的哈希码?

如何解决该问题?

*请注意,该类扩展了AbstractAuditingEntity


@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class AbstractAuditingEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @CreatedBy
    @Column(name = "created_by",nullable = false,length = 50,updatable = false)
    @JsonIgnore
    private String createdBy;

    @CreatedDate
    @Column(name = "created_date",updatable = false)
    @JsonIgnore
    private Instant createdDate = Instant.Now();

    @LastModifiedBy
    @Column(name = "last_modified_by",length = 50)
    @JsonIgnore
    private String lastModifiedBy;

    @LastModifiedDate
    @Column(name = "last_modified_date")
    @JsonIgnore
    private Instant lastModifiedDate = Instant.Now();

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Instant getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Instant createdDate) {
        this.createdDate = createdDate;
    }

    public String getLastModifiedBy() {
        return lastModifiedBy;
    }

    public void setLastModifiedBy(String lastModifiedBy) {
        this.lastModifiedBy = lastModifiedBy;
    }

    public Instant getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Instant lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...