Spring Data no Cassandra com Easy-Cassandra

Spring is an open source framework whose goals is facilitate the Java developer's life, it began with dependency injection and nowadays works on many plug-ins. Plug-ins such Spring Social, to connects social media like twitter and facebbok, Spring security beyond control inversion. The goal of this post is talk about Spring Data working with Cassandra.

Spring Data does part of Spring plug-in, it has as objective do a high level among a data base and Java application. This way, you can use POJO for persist in many kind of data bases.

  • RMDB with JPA and JDBC
  • BigData with Apache Hadoop
  • Data-Grid with GermFire
  • HTTP with Rest
  • NOSQL with MongoDB, Neo4J, Redis and Hbase

The main advantage of Spring data does part of Spring, it may run without Java EE server, it can run a simple container such tomcat or outside of a server. Its dependency injection works together of XML file, but, unlike some developers think, you may use annotations to help, however, you should know when use xml or annotation.

So Spring Data with Cassandra has as objective do a high level layer to NOSQL Cassandra, with POJO structured can do CRUD operations. The Spring Data Cassandra has not launched, but Easy-cassandra has integration with Spring, now you can use Cassandra with most popular frameworks around world.

The application will really simple, it does put information and retrieve by id. Firstly, make the Person entity, our POJO.

@Entity(name = "person") 
public class Person implements Serializable { 

    private UUID id; 
    @Column(name = "name") 
    private String name; 
    @Column(name = "born") 
    private Integer year;
//getter and setter


POJO entity Peron

How you can see, the entity had mapped by JPA 20, secondly is configuration with XML file, within has information of Cassandra Base (base, port, default keySpace) is our connections factory Cassandra inside Spring, also was included the mapped class.

<beans xmlns="http://www.springframework.org/schema/beans"

<context:component-scan base-package="org.javabahia.cassandra.spring" />  
<bean id = "clusterInformation" class="org.easycassandra.persistence.cassandra.ClusterInformation">
    <property name="keySpace" value="consegi" />
    <property name="hosts">
<bean id="cassandraFactory" class="org.easycassandra.persistence.cassandra.spring.CassandraFactoryAnnotation"
    <constructor-arg name="clusterInformation" value="clusterInformation" />
    <property name="annotatedClasses">

Spring Data configuration, that informs configurations of Cassandra (name of host, number of port, default keyspace and mapped class) beyond Cassandra's Template.

With Cassandra connections factory may return a session, the bridge among Cassandra and Easy-cassandra, also will create CassandraTemplase, it an api to do simple operations on Cassandra..

public interface CassandraTemplate{ 
        <T> T save(T entity); 
        <T> Iterable<T> save(Iterable<T> entities); 
        <T> void delete(T entity); 
        <T> void delete(Iterable<T> entities); 
        <K> void delete(K key, Class<?> entity); 
        <K,T> void delete(Iterable<K>  keys, Class<T> entity); 

        <T> void deleteAll(Class<T> entity); 
        <T> T update(T entity); 
        <T> Iterable<T> update(Iterable<T> entities); 
        <T,K> T findOne(K key, Class<T> entity); 
        <T,K> List<T> findAll(Iterable<K> keys, Class<T> entity); 
        <T> List<T> findAll(Class<T> entity); 
        <T,I> List<T> findByIndex(String columnName,I index,Class<T> entity); 
        <K,T>boolean exist(K key, Class<T> entity); 

        void executeUpdate(String query); 

CassandraTemplase's operations

Also there is CassandraReposiroty, this abstract class implements CrudRepository of SpringData, to use it is necessary implement the method which returns CassandraTemplate.

public class PersonRepository extends CassandraRepository<Person, UUID>{

    private CassandraTemplate cassandraTemplate;
    protected CassandraTemplate getCassandraTemplate() {
        return cassandraTemplate;


PersonRepository with CassandraRepository


Whereby CassandraRepository implements the base of persistence on Spring Data, consequently, is available use this operations on Cassandra.

public interface CrudRepository<T, ID extends Serializable> extends Repository<T, ID> { 
    <S extends T> S save(S entity); 

    <S extends T> Iterable<S> save(Iterable<S> entities); 

    T findOne(ID id); 

    boolean exists(ID id); 

    Iterable<T> findAll(); 

    Iterable<T> findAll(Iterable<ID> ids); 

    long count(); 

    void delete(ID id); 

    void delete(T entity); 
    void delete(Iterable<? extends T> entities); 

    void deleteAll(); 

CrudRepository's Interface of Spring Data


Finally run this project.

public class App 
    public static void main( String[] args )
        ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
        PersonRepository personService=ctx.getBean(PersonRepository.class);
        UUID uuid=UUID.randomUUID();
        Person person=new Person();