@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
