Class CassandraTemplate

java.lang.Object
org.springframework.data.cassandra.core.CassandraTemplate
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationEventPublisherAware, CassandraOperations, ExecutableDeleteOperation, ExecutableInsertOperation, ExecutableSelectOperation, ExecutableUpdateOperation, FluentCassandraOperations
Direct Known Subclasses:
CassandraAdminTemplate

public class CassandraTemplate extends Object implements CassandraOperations, org.springframework.context.ApplicationEventPublisherAware, org.springframework.context.ApplicationContextAware
Primary implementation of CassandraOperations. It simplifies the use of Cassandra usage and helps to avoid common errors. It executes core Cassandra workflow. This class executes CQL queries or updates, initiating iteration over ResultSet and catching Cassandra exceptions and translating them to the generic, more informative exception hierarchy defined in the org.springframework.dao package.

Can be used within a service implementation via direct instantiation with a CqlSession reference, or get prepared in an application context and given to services as bean reference.

This class supports the use of prepared statements when enabling setUsePreparedStatements(boolean). All statements created by methods of this class (such as select(Query, Class) or update(Query, org.springframework.data.cassandra.core.query.Update, Class) will be executed as prepared statements. Also, statements accepted by methods (such as select(String, Class) or and others) will be prepared prior to execution. Note that Statement objects passed to methods must be SimpleStatement so that these can be prepared.

Note: The CqlSession should always be configured as a bean in the application context, in the first case given to the service directly, in the second case to the prepared template.

Since:
2.0
Author:
Mark Paluch, John Blum, Lukasz Antoniak, Sam Lightfoot
See Also:
  • Constructor Details

  • Method Details

    • batchOps

      public CassandraBatchOperations batchOps(com.datastax.oss.driver.api.core.cql.BatchType batchType)
      Description copied from interface: CassandraOperations
      Returns a new CassandraBatchOperations. Each CassandraBatchOperations instance can be executed only once, so you need to obtain new CassandraBatchOperations instances for each batch.
      Specified by:
      batchOps in interface CassandraOperations
      Parameters:
      batchType - must not be null.
      Returns:
      a new ReactiveCassandraBatchOperations associated with the given entity class.
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
      Specified by:
      setApplicationEventPublisher in interface org.springframework.context.ApplicationEventPublisherAware
    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) throws org.springframework.beans.BeansException
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • setEntityCallbacks

      public void setEntityCallbacks(@Nullable org.springframework.data.mapping.callback.EntityCallbacks entityCallbacks)
      Configure EntityCallbacks to pre-/post-process entities during persistence operations.
      Parameters:
      entityCallbacks -
    • setEntityLifecycleEventsEnabled

      public void setEntityLifecycleEventsEnabled(boolean enabled)
      Configure whether lifecycle events such as AfterLoadEvent, BeforeSaveEvent, etc. should be published or whether emission should be suppressed. Enabled by default.
      Parameters:
      enabled - true to enable entity lifecycle events; false to disable entity lifecycle events.
      Since:
      4.0
      See Also:
    • getCqlOperations

      public CqlOperations getCqlOperations()
      Description copied from interface: CassandraOperations
      Expose the underlying CqlOperations to allow CQL operations.
      Specified by:
      getCqlOperations in interface CassandraOperations
      Returns:
      the underlying CqlOperations.
      See Also:
    • getConverter

      public CassandraConverter getConverter()
      Description copied from interface: CassandraOperations
      Returns the underlying CassandraConverter.
      Specified by:
      getConverter in interface CassandraOperations
      Returns:
      the underlying CassandraConverter.
    • isUsePreparedStatements

      public boolean isUsePreparedStatements()
      Returns whether this instance is configured to use prepared statements. If enabled (default), then all persistence methods (such as select(java.lang.String, java.lang.Class<T>), update(org.springframework.data.cassandra.core.query.Query, org.springframework.data.cassandra.core.query.Update, java.lang.Class<?>), and others) will make use of prepared statements. Note that methods accepting a Statement must be called with SimpleStatement instances to participate in statement preparation.
      Returns:
      true if prepared statements usage is enabled; false otherwise.
      Since:
      3.2
    • setUsePreparedStatements

      public void setUsePreparedStatements(boolean usePreparedStatements)
      Enable/disable prepared statements usage. If enabled (default), then all persistence methods (such as select(java.lang.String, java.lang.Class<T>), update(org.springframework.data.cassandra.core.query.Query, org.springframework.data.cassandra.core.query.Update, java.lang.Class<?>), and others) will make use of prepared statements. Note that methods accepting a Statement must be called with SimpleStatement instances to participate in statement preparation.
      Parameters:
      usePreparedStatements - whether to use prepared statements.
      Since:
      3.2
    • getEntityOperations

      protected org.springframework.data.cassandra.core.EntityOperations getEntityOperations()
      Returns the EntityOperations used to perform data access operations on an entity inside a Cassandra data source.
      Returns:
      the configured EntityOperations for this template.
      See Also:
      • EntityOperations
    • getProjectionFactory

      protected org.springframework.data.projection.SpelAwareProxyProjectionFactory getProjectionFactory()
      Deprecated.
      Returns a reference to the configured ProjectionFactory used by this template to process CQL query projections.
      Returns:
      a reference to the configured ProjectionFactory used by this template to process CQL query projections.
      Since:
      2.1
      See Also:
      • SpelAwareProxyProjectionFactory
    • getStatementFactory

      protected StatementFactory getStatementFactory()
      Returns the StatementFactory used by this template to construct and run Cassandra CQL statements.
      Returns:
      the StatementFactory used by this template to construct and run Cassandra CQL statements.
      Since:
      2.1
      See Also:
    • getTableName

      public com.datastax.oss.driver.api.core.CqlIdentifier getTableName(Class<?> entityClass)
      Description copied from interface: CassandraOperations
      The table name used for the specified class by this template.
      Specified by:
      getTableName in interface CassandraOperations
      Parameters:
      entityClass - The entity type must not be null.
      Returns:
      the CqlIdentifier
    • select

      public <T> List<T> select(String cql, Class<T> entityClass)
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting items to a List of entities.
      Specified by:
      select in interface CassandraOperations
      Parameters:
      cql - must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted results
    • selectOne

      public <T> T selectOne(String cql, Class<T> entityClass)
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting item to an entity.
      Specified by:
      selectOne in interface CassandraOperations
      Parameters:
      cql - must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted object or null.
    • stream

      public <T> Stream<T> stream(String cql, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting items to a Iterator of entities.

      Returns a Iterator that wraps the Cassandra ResultSet.

      Specified by:
      stream in interface CassandraOperations
      Type Parameters:
      T - element return type.
      Parameters:
      cql - query to execute. Must not be empty or null.
      entityClass - Class type of the elements in the Iterator stream. Must not be null.
      Returns:
      an Iterator (stream) over the elements in the query result set.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • execute

      public com.datastax.oss.driver.api.core.cql.ResultSet execute(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
      Description copied from interface: CassandraOperations
      Execute the given Cassandra Statement. Any errors that result from executing this command will be converted into Spring's DAO exception hierarchy.
      Specified by:
      execute in interface CassandraOperations
      Parameters:
      statement - a Cassandra Statement, must not be null.
      Returns:
      the ResultSet.
    • select

      public <T> List<T> select(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> entityClass)
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting items to a List of entities.
      Specified by:
      select in interface CassandraOperations
      Parameters:
      statement - must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted results
    • selectOne

      public <T> T selectOne(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> entityClass)
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting item to an entity.
      Specified by:
      selectOne in interface CassandraOperations
      Parameters:
      statement - must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted object or null.
    • slice

      public <T> org.springframework.data.domain.Slice<T> slice(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> entityClass)
      Description copied from interface: CassandraOperations
      Execute a SELECT query with paging and convert the result set to a Slice of entities. A sliced query translates the effective fetch size to the page size.
      Specified by:
      slice in interface CassandraOperations
      Parameters:
      statement - the CQL statement, must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted results
    • stream

      public <T> Stream<T> stream(com.datastax.oss.driver.api.core.cql.Statement<?> statement, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting items to a Iterator of entities.

      Returns a Iterator that wraps the Cassandra ResultSet.

      Specified by:
      stream in interface CassandraOperations
      Type Parameters:
      T - element return type.
      Parameters:
      statement - query to execute. Must not be empty or null.
      entityClass - Class type of the elements in the Iterator stream. Must not be null.
      Returns:
      an Iterator (stream) over the elements in the query result set.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • select

      public <T> List<T> select(Query query, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting items to a List of entities.
      Specified by:
      select in interface CassandraOperations
      Parameters:
      query - must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted results
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • selectOne

      public <T> T selectOne(Query query, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting item to an entity.
      Specified by:
      selectOne in interface CassandraOperations
      Parameters:
      query - must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted object or null.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • slice

      public <T> org.springframework.data.domain.Slice<T> slice(Query query, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Execute a SELECT query with paging and convert the result set to a Slice of entities.
      Specified by:
      slice in interface CassandraOperations
      Parameters:
      query - the query object used to create a CQL statement, must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted results
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
      See Also:
    • stream

      public <T> Stream<T> stream(Query query, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Execute a SELECT query and convert the resulting items to a Iterator of entities.

      Returns a Iterator that wraps the Cassandra ResultSet.

      Specified by:
      stream in interface CassandraOperations
      Type Parameters:
      T - element return type.
      Parameters:
      query - query to execute. Must not be empty or null.
      entityClass - Class type of the elements in the Iterator stream. Must not be null.
      Returns:
      an Iterator (stream) over the elements in the query result set.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • update

      public boolean update(Query query, Update update, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Update the queried entities and return true if the update was applied.
      Specified by:
      update in interface CassandraOperations
      Parameters:
      query - must not be null.
      update - must not be null.
      entityClass - The entity type must not be null.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • delete

      public boolean delete(Query query, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Remove entities (rows)/columns from the table by Query.
      Specified by:
      delete in interface CassandraOperations
      Parameters:
      query - must not be null.
      entityClass - The entity type must not be null.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • count

      public long count(Class<?> entityClass)
      Description copied from interface: CassandraOperations
      Returns the number of rows for the given entity class.
      Specified by:
      count in interface CassandraOperations
      Parameters:
      entityClass - type of the entity; must not be null.
      Returns:
      the number of existing entities.
    • count

      public long count(Query query, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Returns the number of rows for the given entity class applying Query. This overridden method allows users to further refine the selection criteria using a Query predicate to determine how many entities of the given type match the criteria.
      Specified by:
      count in interface CassandraOperations
      Parameters:
      query - user-defined count Query to execute; must not be null.
      entityClass - type of the entity; must not be null.
      Returns:
      the number of existing entities.
      Throws:
      org.springframework.dao.DataAccessException - if any problem occurs while executing the query.
    • exists

      public boolean exists(Object id, Class<?> entityClass)
      Description copied from interface: CassandraOperations
      Determine whether a row of entityClass with the given id exists.
      Specified by:
      exists in interface CassandraOperations
      Parameters:
      id - Id value. For single primary keys it's the plain value. For composite primary keys either, it's an instance of either PrimaryKeyClass or MapId. Must not be null.
      entityClass - type of the entity; must not be null.
      Returns:
      true if the object exists.
    • exists

      public boolean exists(Query query, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: CassandraOperations
      Determine whether the result for entityClass Query yields at least one row.
      Specified by:
      exists in interface CassandraOperations
      Parameters:
      query - user-defined exists Query to execute; must not be null.
      entityClass - type of the entity; must not be null.
      Returns:
      true if the object exists.
      Throws:
      org.springframework.dao.DataAccessException - if any problem occurs while executing the query.
    • selectOneById

      public <T> T selectOneById(Object id, Class<T> entityClass)
      Description copied from interface: CassandraOperations
      Execute the Select by id for the given entityClass.
      Specified by:
      selectOneById in interface CassandraOperations
      Parameters:
      id - the Id value. For single primary keys it's the plain value. For composite primary keys either the PrimaryKeyClass or MapId. Must not be null.
      entityClass - The entity type must not be null.
      Returns:
      the converted object or null.
    • insert

      public <T> T insert(T entity)
      Description copied from interface: CassandraOperations
      Insert the given entity and return the entity if the insert was applied.
      Specified by:
      insert in interface CassandraOperations
      Parameters:
      entity - The entity to insert, must not be null.
      Returns:
      the inserted entity.
    • insert

      public <T> EntityWriteResult<T> insert(T entity, InsertOptions options)
      Description copied from interface: CassandraOperations
      Insert the given entity applying WriteOptions and return the entity if the insert was applied.
      Specified by:
      insert in interface CassandraOperations
      Parameters:
      entity - The entity to insert, must not be null.
      options - must not be null.
      Returns:
      the EntityWriteResult for this operation.
      See Also:
    • update

      public <T> T update(T entity)
      Description copied from interface: CassandraOperations
      Update the given entity and return the entity if the update was applied.
      Specified by:
      update in interface CassandraOperations
      Parameters:
      entity - The entity to update, must not be null.
      Returns:
      the updated entity.
    • update

      public <T> EntityWriteResult<T> update(T entity, UpdateOptions options)
      Description copied from interface: CassandraOperations
      Update the given entity applying WriteOptions and return the entity if the update was applied.
      Specified by:
      update in interface CassandraOperations
      Parameters:
      entity - The entity to update, must not be null.
      options - must not be null.
      Returns:
      the EntityWriteResult for this operation.
      See Also:
    • delete

      public void delete(Object entity)
      Description copied from interface: CassandraOperations
      Delete the given entity and return the entity if the delete statement was applied.
      Specified by:
      delete in interface CassandraOperations
      Parameters:
      entity - must not be null.
    • delete

      public WriteResult delete(Object entity, QueryOptions options)
      Description copied from interface: CassandraOperations
      Delete the given entity applying QueryOptions and return the entity if the delete statement was applied.
      Specified by:
      delete in interface CassandraOperations
      Parameters:
      entity - must not be null.
      options - must not be null.
      Returns:
      the WriteResult for this operation.
      See Also:
    • deleteById

      public boolean deleteById(Object id, Class<?> entityClass)
      Description copied from interface: CassandraOperations
      Remove the given object from the table by id.
      Specified by:
      deleteById in interface CassandraOperations
      Parameters:
      id - the Id value. For single primary keys it's the plain value. For composite primary keys either the PrimaryKeyClass or MapId. Must not be null.
      entityClass - The entity type must not be null.
    • truncate

      public void truncate(Class<?> entityClass)
      Description copied from interface: CassandraOperations
      Execute a TRUNCATE query to remove all entities of a given class.
      Specified by:
      truncate in interface CassandraOperations
      Parameters:
      entityClass - The entity type must not be null.
    • query

      public <T> ExecutableSelectOperation.ExecutableSelect<T> query(Class<T> domainType)
      Description copied from interface: ExecutableSelectOperation
      Begin creating a Cassandra SELECT query operation for the given domainType.
      Specified by:
      query in interface ExecutableSelectOperation
      Type Parameters:
      T - type of the application domain object.
      Parameters:
      domainType - type to domain object to query; must not be null.
      Returns:
      new instance of ExecutableSelectOperation.ExecutableSelect.
      See Also:
    • insert

      public <T> ExecutableInsertOperation.ExecutableInsert<T> insert(Class<T> domainType)
      Description copied from interface: ExecutableInsertOperation
      Begin creating an INSERT operation for given domainType.
      Specified by:
      insert in interface ExecutableInsertOperation
      Parameters:
      domainType - type of domain object to insert; must not be null.
      Returns:
      new instance of ExecutableInsertOperation.ExecutableInsert.
      See Also:
    • update

      public ExecutableUpdateOperation.ExecutableUpdate update(Class<?> domainType)
      Description copied from interface: ExecutableUpdateOperation
      Begin creating an UPDATE operation for the given domainType.
      Specified by:
      update in interface ExecutableUpdateOperation
      Parameters:
      domainType - type of domain object to update; must not be null.
      Returns:
      new instance of ExecutableUpdateOperation.ExecutableUpdate.
    • delete

      public ExecutableDeleteOperation.ExecutableDelete delete(Class<?> domainType)
      Description copied from interface: ExecutableDeleteOperation
      Begin creating a DELETE operation for the given domainType.
      Specified by:
      delete in interface ExecutableDeleteOperation
      Parameters:
      domainType - type of domain object to delete; must not be null.
      Returns:
      new instance of ExecutableDeleteOperation.ExecutableDelete.
      See Also:
    • createPreparedStatementHandler

      protected CassandraTemplate.PreparedStatementHandler createPreparedStatementHandler(com.datastax.oss.driver.api.core.cql.Statement<?> statement)
      Create a new statement-based CassandraTemplate.PreparedStatementHandler using the statement passed in.

      This method allows for the creation to be overridden by subclasses.

      Parameters:
      statement - the statement to be prepared.
      Returns:
      the new CassandraTemplate.PreparedStatementHandler to use.
      Since:
      3.3.3
    • maybeEmitEvent

      protected <E extends CassandraMappingEvent<T>, T> void maybeEmitEvent(Supplier<E> event)
    • maybeCallBeforeConvert

      protected <T> T maybeCallBeforeConvert(T object, com.datastax.oss.driver.api.core.CqlIdentifier tableName)
    • maybeCallBeforeSave

      protected <T> T maybeCallBeforeSave(T object, com.datastax.oss.driver.api.core.CqlIdentifier tableName, com.datastax.oss.driver.api.core.cql.Statement<?> statement)