在spring-data-cassandra的PrimaryKeyClass中实现hashCode

时间:2015-03-05 04:36:16

标签: java spring cassandra spring-data spring-data-cassandra

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

实施hashcode&的重要性我班级的equals方法。 上述hashcode

@PrimaryKeyClass实施应该是什么

1 个答案:

答案 0 :(得分: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);
}