I try using Cassandra for persisting quite simple POJOs, but using a class hierarchy (many subclasses, one superclass). I am using Java and the Datastax driver (and the object mapper).
My problem is that the Object Mapper doesn't seem to recognize fields annotated in superclasses.
The structure I implemented is:
@Table(keyspace = "...", name = "...")
public class Subclass extends Superclass {
public double x;
public double y;
public double z;
....
}
public class Superclass {
@PartitionKey(0)
@Column(name = "user_id")
public long userId;
@PartitionKey(1)
@Column(name = "device_id")
public long deviceId;
}
I then try to save Subclass objects:
public static void main(String[] args) {
Subclass data = new Subclass();
data.deviceId = 123;
data.userId = 1212;
data.x = 0;
data.y = 1;
data.z = 2;
Cluster cluster;
Session session;
...
Mapper<Subclass> mapper = new MappingManager(session).mapper(Subclass.class);
mapper.save(data);
}
This throws:
com.datastax.driver.core.exceptions.InvalidQueryException: Missing mandatory PRIMARY KEY part user_id
Is there any way to get it running in a structure like this? One solution was to redeclare the userId / deviceID field in the subclass, but this would be quite ugly (much repetitive code). Any ideas how to archive a good structure or best practices for a case like this?