【问题标题】:Do I need to override equals() and hashCode() in the IdClass (composed key) of a jpa Entity我是否需要覆盖 jpa 实体的 IdClass (组合键)中的 equals() 和 hashCode()
【发布时间】:2017-10-05 10:51:27
【问题描述】:

如果我在 Jpa 实体 (MyEntity) 中有组合键,是否需要在 IdClass(本例中为 ID)中添加 equals() 和 hashCode()?它被视为重复?或者在 MyEntity “equals and hashCode()”中,我必须调用那些 ID 类 [return new MyEntity.ID(id1,id2).hashCode();在 MyEntity] 的 hashCode() 中?

@Entity
@IdClass(MyEntity.ID.class)
public class MyEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    private long id1;
    @Id
    private long id2;

    private String otherField;
    public static class ID implements Serializable {
        private static final long serialVersionUID = 1L;
        private long id1;
        private long id2;
        public ID() {
            super();
        }
        public ID(long id1, long id2) {
            super();
            this.id1 = id1;
            this.id2 = id2;
        }
        public long getId1() {
            return id1;
        }
        public void setId1(long id1) {
            this.id1 = id1;
        }
        public long getId2() {
            return id2;
        }
        public void setId2(long id2) {
            this.id2 = id2;
        }
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (int) (id1 ^ (id1 >>> 32));
            result = prime * result + (int) (id2 ^ (id2 >>> 32));
            return result;
        }
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            ID other = (ID) obj;
            if (id1 != other.id1) {
                return false;
            }
            if (id2 != other.id2) {
                return false;
            }
            return true;
        }
    }
    public MyEntity() {
        super();
    }
    public long getId1() {
        return id1;
    }
    public void setId1(long id1) {
        this.id1 = id1;
    }
    public long getId2() {
        return id2;
    }
    public void setId2(long id2) {
        this.id2 = id2;
    }
    public String getOtherField() {
        return otherField;
    }
    public void setOtherField(String otherField) {
        this.otherField = otherField;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (int) (id1 ^ (id1 >>> 32));
        result = prime * result + (int) (id2 ^ (id2 >>> 32));
        result = prime * result + ((otherField == null) ? 0 : otherField.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        MyEntity other = (MyEntity) obj;
        if (id1 != other.id1) {
            return false;
        }
        if (id2 != other.id2) {
            return false;
        }
        if (otherField == null) {
            if (other.otherField != null) {
                return false;
            }
        } else if (!otherField.equals(other.otherField)) {
            return false;
        }
        return true;
    }
}

【问题讨论】:

  • 我会在两个课程中都说

标签: java hibernate jpa


【解决方案1】:

1.

是否需要在IdClass(本例中为ID)中添加equals()和hashCode()?

是的,你确实需要它。这里是the documentation

复合主键类具有以下特点:

...

  • 它定义了equals和hashCode方法。这些方法的值相等语义必须与键映射到的数据库类型的数据库相等性一致。

...

2.

或者在 MyEntity "equals and hashCode()" 中,我必须调用那些 ID 类 [return new MyEntity.ID(id1,id2).hashCode();在 MyEntity] 的 hashCode() 中?

我不确定您的实际意思,但您不需要构造 ID 类的新实例。只需遵循 the documentation 中的 equals & hasCode 合同规则即可。 (在您的 IDE 中自动生成 equals 和 hasCode 也可以正常工作)。

【讨论】:

    【解决方案2】:

    请看这个问题JPA/Hibernate “Composite-id class does not override equals()”

    以下是有关equals() and hashCode()的文档中的一些项目

    我们建议使用 业务密钥来实现 equals()hashCode() 平等。业务密钥相等意味着equals() 方法 仅比较构成业务密钥的属性,该密钥 将识别我们在现实世界中的实例(一个自然候选 键)

    equals/hashCode 的问题不是小事,也没有一刀切的解决方案。

    虽然对于 equals 和 hashCode 使用自然 ID 是最好的,但有时 您只有提供唯一约束的实体标识符。

    可以使用实体标识符进行相等检查,但它 需要解决方法:

    您需要为 hashCode 提供一个常量值,以便哈希 实体刷新前后代码值不变。

    您只需要比较实体标识符是否相等 非瞬态实体。

    也是一篇好文章EqualsandHashCode

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2020-08-26
      • 2013-06-02
      • 2014-07-01
      • 1970-01-01
      • 2019-01-22
      相关资源
      最近更新 更多