A good example for this can be found in the Git repo for DataStax's E-commerce workshop.
Consider this table/PK definition:
CREATE TABLE user (
user_id UUID,
...
PRIMARY KEY (user_id));
As there is a single column defined as the primary key, the UserEntity class only needs to use the @PrimaryKey annotation:
@PrimaryKey("user_id")
private UUID userId;
Now consider this table/PK definition:
CREATE TABLE cart_products (
cart_id uuid,
product_timestamp timestamp,
product_id text,
...
PRIMARY KEY (cart_id, product_timestamp, product_id)
) WITH CLUSTERING ORDER BY (product_timestamp DESC, product_id ASC)
AND default_time_to_live = 5184000;
cart_products uses three columns for its PRIMARY KEY: cart_id as the partition key; product_timestamp and product_id for the clustering keys. So the CartProductEntity class still uses the @PrimaryKey annotation, but it references the CartProductsPrimaryKey class:
@PrimaryKey
private CartProductsPrimaryKey key;
Inside that class, the individual primary key columns are annotated with @PrimaryKeyColumn:
@PrimaryKeyClass
public class CartProductsPrimaryKey {
@PrimaryKeyColumn(
name = "cart_id",
ordinal = 0,
type = PrimaryKeyType.PARTITIONED)
private UUID cartId;
@PrimaryKeyColumn(
name = "product_timestamp",
ordinal = 1,
type = PrimaryKeyType.CLUSTERED,
ordering = Ordering.DESCENDING)
private Date productTimestamp;
@PrimaryKeyColumn(
name = "product_id",
ordinal = 2,
type = PrimaryKeyType.CLUSTERED,
ordering = Ordering.ASCENDING)
private String productId;
Basically, the annotations used depend largely on the complexity of the primary key definition.
As for the @Id annotation, it serves the same function as @PrimaryKey. This is mentioned in the Spring Data Cassandra repository.
The @Id annotation tells the mapper which property you want to use for the Cassandra primary key. Composite primary keys can require a slightly different data model.