0

我正在使用spring-data-cassandra并且有一个表,它的主键是((int_col1,int_col2),bigint_col1,bigint_col2). int_col1&int_col2是分区键 bigint_col1&bigint_col2是集群键。

hashcode为我的班级实施&equals方法有多重要。hashcode我上面的执行应该是什么@PrimaryKeyClass

4

1 回答 1

1
// your class's constructor should have exactly four arguments
// and ensure that each of these four fields are non-null

@Override
public int hashCode() {
  return 37
    ^ int_col1.hashCode()
    ^ int_col2.hashCode()
    ^ bigint_col1.hashCode()
    ^ bigint_col2.hashCode();
}

@Override
public boolean equals(Object that) {
  if (this == that) {
    return true;
  }
  if (that == null) {
    return false;
  }
  if (!(that instanceof YourPrimaryKeyClass)) {
    return false;
  }
  YourPrimaryKeyClass other = (YourPrimaryKeyClass) that;
  return this.int_col1.equals(other.int_col1)
    && this.int_col2.equals(other.int_col2)
    && bigint_col1.equals(other.bigint_col1)
    && bigint_col2.equals(other.bigint_col2);
}
于 2015-03-05T15:39:30.537 回答