Home > Blockchain >  What is the difference between @PrimaryKeyColumn and @PrimaryKey in Spring Data Cassandra?
What is the difference between @PrimaryKeyColumn and @PrimaryKey in Spring Data Cassandra?

Time:04-15

I have recently started using Cassandra for my Spring Boot applications. I have always just used @PrimaryKeyColumn and @Id annotation to mark the Primary Key in the Java Class but yesterday I saw a github repos where people were using @PrimaryKeyColumn annotations with @Id annotation and some other repos where people are just using @PrimaryKey annotations. What am I missing here, I feel so lost.

CodePudding user response:

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.

CodePudding user response:

https://github.com/spring-projects/spring-data-cassandra/blob/main/src/main/asciidoc/reference/mapping.adoc#metadata-based-mapping

@Id: Applied at the field or property level to mark the property used for identity purposes.

@PrimaryKey: Similar to @Id but lets you specify the column name.

@PrimaryKeyColumn: Cassandra-specific annotation for primary key columns that lets you specify primary key column attributes, such as for clustered or partitioned. Can be used on single and multiple attributes to indicate either a single or a composite (compound) primary key. If used on a property within the entity, make sure to apply the @Id annotation as well.

@PrimaryKeyClass: Applied at the class level to indicate that this class is a compound primary key class. Must be referenced with

  • Related