Apache Cassandra Entities and Repositories
Learn about entities and repositories through the Spring Data Cassandra framework.
We'll cover the following...
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.
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@Tablepublic class Book {@PrimaryKeyprivate UUID id;@Columnprivate String name;@Columnprivate String summary;@Overridepublic String toString() {return "Book{" +"id=" + id +", name='" + name + '\'' +", summary='" + summary + '\'' +'}';}}
Code explanation:
-
Line 11: We use Lombok’s
@Dataannotation to automatically create the getters/setters of the properties,equals, andhashcodemethods. -
Line 12: We use the
@Tableannotation of Spring Data Cassandra to map theBookentity with thebooktable in our Cassandra database. -
Lines 15 and 16: We define the
UUIDtypeidand use the@PrimaryKeyannotation to declare the primary key field of theBookentity. -
Lines 18–22: ...