0

I am using spring-data-cassandra and have a table such that its primary key is ((int_col1,int_col2),bigint_col1,bigint_col2). int_col1&int_col2 are the partition keys bigint_col1 & bigint_col2 are the cluster keys.

How important is it to implement hashcode & equals method for my class. What should be the hashcode implementation of my above @PrimaryKeyClass

jny
  • 7,739
  • 3
  • 30
  • 53
S Kr
  • 1,772
  • 2
  • 23
  • 48

1 Answers1

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);
}
Matthew Adams
  • 1,826
  • 2
  • 19
  • 27