【发布时间】:2020-06-19 08:54:24
【问题描述】:
我有一个包含复合主键的表,包括它自己的和来自其他表的 FK。
我决定不使用@IdClass 或@EmbeddedId。
class SomeEntity {
@Id
private String someId;
@Id
@ManyToOne(optional = false)
@JoinColumn(name = ...)
private Other other
// other mappings here
}
Hibernate 会警告这些。
WARN 69752 ... : HHH000038: Composite-id class does not override equals(): ....Some
WARN 69752 ... : HHH000039: Composite-id class does not override hashCode(): ....Some
我如何(应该)实现这两种方法?
我可以(应该)只包括这两个字段吗?其他领域呢?
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Some that = (Some) o;
return Objects.equals(someId, that.someId) &&
Objects.equals(other, that.other);
}
@Override
public int hashCode() {
return Objects.hash(someId, other);
}
【问题讨论】:
-
@SternK 感谢您的链接。不幸的是,我认为这是相同的上下文,而不是相同的情况。
标签: hibernate jpa equals hashcode composite-primary-key