Class R2dbcEntityTemplate

java.lang.Object
org.springframework.data.r2dbc.core.R2dbcEntityTemplate
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.context.ApplicationContextAware, FluentR2dbcOperations, R2dbcEntityOperations, ReactiveDeleteOperation, ReactiveInsertOperation, ReactiveSelectOperation, ReactiveUpdateOperation

public class R2dbcEntityTemplate extends Object implements R2dbcEntityOperations, org.springframework.beans.factory.BeanFactoryAware, org.springframework.context.ApplicationContextAware
Implementation of R2dbcEntityOperations. It simplifies the use of Reactive R2DBC usage through entities and helps to avoid common errors. This class uses DatabaseClient to execute SQL queries or updates, initiating iteration over Result.

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

Since:
1.1
Author:
Mark Paluch, Bogdan Ilchyshyn, Jens Schauder, Jose Luis Leon, Robert Heim
  • Constructor Details

    • R2dbcEntityTemplate

      public R2dbcEntityTemplate(io.r2dbc.spi.ConnectionFactory connectionFactory)
      Create a new R2dbcEntityTemplate given ConnectionFactory.
      Parameters:
      connectionFactory - must not be null.
      Since:
      1.2
    • R2dbcEntityTemplate

      public R2dbcEntityTemplate(org.springframework.r2dbc.core.DatabaseClient databaseClient, R2dbcDialect dialect)
      Create a new R2dbcEntityTemplate given DatabaseClient.
      Parameters:
      databaseClient - must not be null.
      dialect - the dialect to use, must not be null.
      Since:
      1.2
    • R2dbcEntityTemplate

      public R2dbcEntityTemplate(org.springframework.r2dbc.core.DatabaseClient databaseClient, R2dbcDialect dialect, R2dbcConverter converter)
      Create a new R2dbcEntityTemplate given DatabaseClient, R2dbcDialect and R2dbcConverter.
      Parameters:
      databaseClient - must not be null.
      dialect - the dialect to use, must not be null.
      converter - the dialect to use, must not be null.
      Since:
      1.2
    • R2dbcEntityTemplate

      public R2dbcEntityTemplate(org.springframework.r2dbc.core.DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy)
      Create a new R2dbcEntityTemplate given DatabaseClient and ReactiveDataAccessStrategy.
      Parameters:
      databaseClient - must not be null.
      Since:
      1.2
  • Method Details

    • getDatabaseClient

      public org.springframework.r2dbc.core.DatabaseClient getDatabaseClient()
      Description copied from interface: R2dbcEntityOperations
      Expose the underlying DatabaseClient to allow SQL operations.
      Specified by:
      getDatabaseClient in interface R2dbcEntityOperations
      Returns:
      the underlying DatabaseClient.
      See Also:
      • DatabaseClient
    • getDataAccessStrategy

      public ReactiveDataAccessStrategy getDataAccessStrategy()
      Description copied from interface: R2dbcEntityOperations
      Expose the underlying ReactiveDataAccessStrategy encapsulating dialect specifics.
      Specified by:
      getDataAccessStrategy in interface R2dbcEntityOperations
      Returns:
      the underlying ReactiveDataAccessStrategy.
      See Also:
    • getConverter

      public R2dbcConverter getConverter()
      Description copied from interface: R2dbcEntityOperations
      Return the underlying R2dbcConverter.
      Specified by:
      getConverter in interface R2dbcEntityOperations
      Returns:
      the underlying R2dbcConverter.
    • setBeanFactory

      @Deprecated public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) throws org.springframework.beans.BeansException
      Deprecated.
      Specified by:
      setBeanFactory in interface org.springframework.beans.factory.BeanFactoryAware
      Throws:
      org.springframework.beans.BeansException
    • 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(org.springframework.data.mapping.callback.ReactiveEntityCallbacks entityCallbacks)
      Set the ReactiveEntityCallbacks instance to use when invoking callbacks like the BeforeSaveCallback.

      Overrides potentially existing ReactiveEntityCallbacks.

      Parameters:
      entityCallbacks - must not be null.
      Throws:
      IllegalArgumentException - if the given instance is null.
      Since:
      1.2
    • select

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

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

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

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

      public reactor.core.publisher.Mono<Long> count(Query query, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Returns the number of rows for the given entity class applying Query. This overridden method allows users to further refine the selection Query using a Query predicate to determine how many entities of the given type match the Query.
      Specified by:
      count in interface R2dbcEntityOperations
      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 reactor.core.publisher.Mono<Boolean> exists(Query query, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Determine whether the result for entityClass Query yields at least one row.
      Specified by:
      exists in interface R2dbcEntityOperations
      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.
    • select

      public <T> reactor.core.publisher.Flux<T> select(Query query, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Execute a SELECT query and convert the resulting items to a stream of entities.
      Specified by:
      select in interface R2dbcEntityOperations
      Parameters:
      query - must not be null.
      entityClass - the entity type must not be null.
      Returns:
      the result objects returned by the action.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem issuing the execution.
    • selectOne

      public <T> reactor.core.publisher.Mono<T> selectOne(Query query, Class<T> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Execute a SELECT query and convert the resulting item to an entity ensuring exactly one result.
      Specified by:
      selectOne in interface R2dbcEntityOperations
      Parameters:
      query - must not be null.
      entityClass - the entity type must not be null.
      Returns:
      exactly one result or Mono.empty() if no match found.
      Throws:
      org.springframework.dao.IncorrectResultSizeDataAccessException - if more than one match found.
      org.springframework.dao.DataAccessException - if there is any problem issuing the execution.
    • update

      public reactor.core.publisher.Mono<Long> update(Query query, Update update, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Update the queried entities and return true if the update was applied.
      Specified by:
      update in interface R2dbcEntityOperations
      Parameters:
      query - must not be null.
      update - must not be null.
      entityClass - the entity type must not be null.
      Returns:
      the number of affected rows.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem executing the query.
    • delete

      public reactor.core.publisher.Mono<Long> delete(Query query, Class<?> entityClass) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Remove entities (rows)/columns from the table by Query.
      Specified by:
      delete in interface R2dbcEntityOperations
      Parameters:
      query - must not be null.
      entityClass - the entity type must not be null.
      Returns:
      the number of affected rows.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem issuing the execution.
    • query

      public <T> org.springframework.r2dbc.core.RowsFetchSpec<T> query(org.springframework.r2dbc.core.PreparedOperation<?> operation, Class<T> entityClass)
      Description copied from interface: R2dbcEntityOperations
      Execute a query for a RowsFetchSpec, given PreparedOperation. Any provided bindings within PreparedOperation are applied to the underlying DatabaseClient. The query is issued as-is without additional pre-processing such as named parameter expansion. Results of the query are mapped onto entityClass.
      Specified by:
      query in interface R2dbcEntityOperations
      Parameters:
      operation - the prepared operation wrapping a SQL query and bind parameters.
      entityClass - the entity type must not be null.
      Returns:
      a RowsFetchSpec ready to materialize.
    • query

      public <T> org.springframework.r2dbc.core.RowsFetchSpec<T> query(org.springframework.r2dbc.core.PreparedOperation<?> operation, BiFunction<io.r2dbc.spi.Row,io.r2dbc.spi.RowMetadata,T> rowMapper)
      Description copied from interface: R2dbcEntityOperations
      Execute a query for a RowsFetchSpec, given PreparedOperation. Any provided bindings within PreparedOperation are applied to the underlying DatabaseClient. The query is issued as-is without additional pre-processing such as named parameter expansion. Results of the query are mapped using rowMapper.
      Specified by:
      query in interface R2dbcEntityOperations
      Parameters:
      operation - the prepared operation wrapping a SQL query and bind parameters.
      rowMapper - the row mapper must not be null.
      Returns:
      a RowsFetchSpec with rowMapper applied ready to materialize.
    • query

      public <T> org.springframework.r2dbc.core.RowsFetchSpec<T> query(org.springframework.r2dbc.core.PreparedOperation<?> operation, Class<?> entityClass, BiFunction<io.r2dbc.spi.Row,io.r2dbc.spi.RowMetadata,T> rowMapper)
      Description copied from interface: R2dbcEntityOperations
      Execute a query for a RowsFetchSpec in the context of entityClass, given PreparedOperation. Any provided bindings within PreparedOperation are applied to the underlying DatabaseClient. The query is issued as-is without additional pre-processing such as named parameter expansion. Results of the query are mapped using rowMapper.
      Specified by:
      query in interface R2dbcEntityOperations
      Parameters:
      operation - the prepared operation wrapping a SQL query and bind parameters.
      entityClass - the entity type must not be null.
      rowMapper - the row mapper must not be null.
      Returns:
      a RowsFetchSpec with rowMapper applied ready to materialize.
      See Also:
    • insert

      public <T> reactor.core.publisher.Mono<T> insert(T entity) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Insert the given entity and emit the entity if the insert was applied.
      Specified by:
      insert in interface R2dbcEntityOperations
      Parameters:
      entity - the entity to insert, must not be null.
      Returns:
      the inserted entity.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem issuing the execution.
    • update

      public <T> reactor.core.publisher.Mono<T> update(T entity) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Update the given entity and emit the entity if the update was applied.
      Specified by:
      update in interface R2dbcEntityOperations
      Parameters:
      entity - the entity to update, must not be null.
      Returns:
      the updated entity.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem issuing the execution.
      org.springframework.dao.TransientDataAccessResourceException - if the update did not affect any rows.
    • delete

      public <T> reactor.core.publisher.Mono<T> delete(T entity) throws org.springframework.dao.DataAccessException
      Description copied from interface: R2dbcEntityOperations
      Delete the given entity and emit the entity if the delete was applied.
      Specified by:
      delete in interface R2dbcEntityOperations
      Parameters:
      entity - must not be null.
      Returns:
      the deleted entity.
      Throws:
      org.springframework.dao.DataAccessException - if there is any problem issuing the execution.
    • maybeCallBeforeConvert

      protected <T> reactor.core.publisher.Mono<T> maybeCallBeforeConvert(T object, SqlIdentifier table)
    • maybeCallBeforeSave

      protected <T> reactor.core.publisher.Mono<T> maybeCallBeforeSave(T object, OutboundRow row, SqlIdentifier table)
    • maybeCallAfterSave

      protected <T> reactor.core.publisher.Mono<T> maybeCallAfterSave(T object, OutboundRow row, SqlIdentifier table)
    • maybeCallAfterConvert

      protected <T> reactor.core.publisher.Mono<T> maybeCallAfterConvert(T object, SqlIdentifier table)