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.
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
@Data
annotation to automatically create the getters/setters of the properties,equals
, andhashcode
methods. -
Line 12: We use the
@Table
annotation of Spring Data Cassandra to map theBook
entity with thebook
table in our Cassandra database. -
Lines 15 and 16: We define the
UUID
typeid
and use the@PrimaryKey
annotation to declare the primary key field of the ...