liveBook · Manning

@primarykeycolumn

As explained in Spring in Action, Sixth Edition

@PrimaryKeyColumn is a Spring Data Cassandra annotation that maps a property to a specifically configured primary-key column.

@PrimaryKeyColumn maps a property to a specifically configured Cassandra primary-key column. Set its type to PARTITIONED for the partition key or CLUSTERED for a clustering key. Partition keys determine where rows are stored; clustering keys determine their order within a partition. section 4.1.3

Mapping a Taco key

A Taco uses a UUID id annotated with @PrimaryKeyColumn(type = PrimaryKeyType.PARTITIONED). Its createdAt property is another primary-key column with CLUSTERED, so newer rows appear first within each partition. section 4.1.3

Listing 4.1 Annotating the Taco class for Cassandra persistence
package tacos;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
 
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
 
import org.springframework.data.cassandra.core.cql.Ordering;
import org.springframework.data.cassandra.core.cql.PrimaryKeyType;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.core.mapping.Table;
 
import com.datastax.oss.driver.api.core.uuid.Uuids;
 
import lombok.Data;
 
@Data
@Table("tacos")                                                    #1
public class Taco {
 
  @PrimaryKeyColumn(type=PrimaryKeyType.PARTITIONED)               #2
  private UUID id = Uuids.timeBased();
 
  @NotNull
  @Size(min = 5, message = "Name must be at least 5 characters long")
  private String name;
 
  @PrimaryKeyColumn(type=PrimaryKeyType.CLUSTERED,                 #3
                    ordering=Ordering.DESCENDING)
  private Date createdAt = new Date();
 
  @Size(min=1, message="You must choose at least 1 ingredient")
  @Column("ingredients")                                           #4
  private List<IngredientUDT> ingredients = new ArrayList<>();
 
  public void addIngredient(Ingredient ingredient) {
    this.ingredients.add(TacoUDRUtils.toIngredientUDT(ingredient));
  }
}

Use the complete Taco mapping to see the annotation alongside the table declaration, UUID identifier, and other Cassandra persistence annotations. section 4.1.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains partition and row-order behavior through a Taco mapping, without descending into Cassandra storage internals.
Examples
Uses Taco id and createdAt properties to demonstrate partitioning and descending clustering order.
What it assumes
Assumes basic domain-class and Cassandra persistence concepts while distinguishing partitioned and clustered keys through example.
What you can do after
Apply the annotation to configure how Cassandra distributes rows and orders them within partitions.
How it connects
Connects Spring Data Cassandra mappings with partition keys, clustering keys, UUID identifiers, and Cassandra tables.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.