javaspringcassandraspring-data-cassandra

How to map domain objects to CQL Tables using Spring Data Cassandra?


I have two model classes:

public class AlertMatchesDTO implements Serializable
{
  private static final long serialVersionUID = -3704734448105124277L;

  @PrimaryKey
  private String alertOid;

  @Column("matches")
  private List<HotelPriceDTO> matches;
...
}

public class HotelPriceDTO implements Serializable
{
  private static final long serialVersionUID = -8751629882750913707L;

  private Long hotelOid;
  private double priceByNight;
  private Date checkIn;
  private Date checkOut;
...
}

and I want to persist instances of the first class in a Cassandra column family using Spring Data. In particular using Cassandra template like this:

...
cassandraTemplate.insert(dto, writeOptions); 
...

and Spring Data have problems serializing List<HotelPriceDTO>. What I think I need is a way to tell cassandraTemplate how to convert the type. In the official documentation, there is a chapter telling that I have to use CassandraMappingConverter and MappingCassandraConverter, but they do not provide an example yet.

My question is: is there an example of how to register a converter like this (in the test code of the project, may be?) or any other example I can use while the official documentation is completed? Thanks in advance.


Solution

  • Hate to say this, but you should RTFM at http://docs.spring.io/spring-data/cassandra/docs/1.1.0.RELEASE/reference/html/.

    Having said that, I noticed the DTO suffixes on your class names, which implies to me that you may not have a domain model, only a service layer with DTOs. If that's the case, you might consider defining the mappings yourself as RowMapper implementations and simply use CqlTemplate without the bells & whistles of Spring Data Cassandra. If you choose to fuse the architectural concepts of DTO and entity (entity being a persistent domain object), you're free to use Spring Data Cassandra along with the mapping metadata required (@Table, @PrimaryKeyColumn, etc). Your choice.