2
votes

I'm creating a heirarchy of classes for the purposes of denormalizing a table. Tables are quite wide so to manage hassle and errors, I would like to declare all fields in the parent and then alter the definition of specific fields in the child. This way I can have a normal column in the parent be redefined as part of a primary key in the child.

E.g.:

Basic super-class

public class A {
  @Column
  protected int age;
}

Class extending A

@Table
public class B extends A {
  @PrimaryKey
  protected K key;
  ...
}

Primary key for A, with a new definition of the age column.

@PrimaryKeyClass
public class K {
  @PrimaryKeyColumn
  private int age;
  @PrimaryKeyColumn
  private int ignore;
}

That doesn't seem to work. I get a Multiple definition of identifier exception.

So far I can only declare fields that won't change in the parent and then declare all possibly-changing fields in each child. I don't like this because I have to know upfront every field that might become part of future primary keys.

Putting the annotations on the (parent) getters and overriding those doesn't seem to have done anything.

Any way to solve this aside from keeping only never-changing columns in the parent?

1

1 Answers

1
votes

Putting the annotations on the (parent) getters and overriding those doesn't seem to have done anything.

Annotating the child getters with @Transient did!

So: annotate the parent getters rather than the fields, make child getters @Transient, move column definition into the key, done.

From here:

http://docs.datastax.com/en/developer/java-driver/3.1/manual/object_mapper/creating/

One powerful advantage of annotating getter methods is that annotations are inherited from overridden methods in superclasses and superinterfaces; in other words, if a getter method is overridden in a subclass, annotations in both method declarations will get merged together. If duplicated annotations are found during this merge process, the overriding method’s annotations will take precedence over the overridden’s.