...

/

Apache Cassandra Entities and Repositories

Apache Cassandra Entities and Repositories

Learn about entities and repositories through the Spring Data Cassandra framework.

Apache Cassandra entities and repositories in Spring Data Cassandra enable seamless interaction with Cassandra databases. Entities map POJO classes to database tables, while repositories provide CRUD operations and query capabilities.

POJOs

Let’s take the same example of the Book and Author POJOs referencing the book and author Cassandra tables and convert them into entity classes.

Create a Book entity

We create the Book POJO class in the com.smartdiscover.model package to map it to the Cassandra book table.

Press + to interact
package com.smartdiscover.model;
import lombok.Data;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
import java.util.Date;
import java.util.UUID;
@Data
@Table
public class Book {
@PrimaryKey
private UUID id;
@Column
private String name;
@Column
private String summary;
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", summary='" + summary + '\'' +
'}';
}
}

Code explanation:

  • Line 11: We use Lombok’s @Data annotation to automatically create the getters/setters of the properties, equals, and hashcode methods.

  • Line 12: We use the @Table annotation of Spring Data Cassandra to map the Book entity with the book table in our Cassandra database.

  • Lines 15 and 16: We define the UUID type id and use the @PrimaryKey annotation to declare the primary key field of the ...