Class R2dbcEntityTemplate
java.lang.Object
org.springframework.data.r2dbc.core.R2dbcEntityTemplate
- All Implemented Interfaces:
Aware,BeanFactoryAware,ApplicationContextAware,FluentR2dbcOperations,R2dbcEntityOperations,ReactiveDeleteOperation,ReactiveInsertOperation,ReactiveSelectOperation,ReactiveUpdateOperation
public class R2dbcEntityTemplate
extends Object
implements R2dbcEntityOperations, BeanFactoryAware, 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, Sebastian Wieland, Mikhail Polivakha
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.data.r2dbc.core.ReactiveDeleteOperation
ReactiveDeleteOperation.DeleteWithQuery, ReactiveDeleteOperation.DeleteWithTable, ReactiveDeleteOperation.ReactiveDelete, ReactiveDeleteOperation.TerminatingDeleteNested classes/interfaces inherited from interface org.springframework.data.r2dbc.core.ReactiveInsertOperation
ReactiveInsertOperation.InsertWithTable<T>, ReactiveInsertOperation.ReactiveInsert<T>, ReactiveInsertOperation.TerminatingInsert<T>Nested classes/interfaces inherited from interface org.springframework.data.r2dbc.core.ReactiveSelectOperation
ReactiveSelectOperation.ReactiveSelect<T>, ReactiveSelectOperation.SelectWithProjection<T>, ReactiveSelectOperation.SelectWithQuery<T>, ReactiveSelectOperation.SelectWithTable<T>, ReactiveSelectOperation.TerminatingSelect<T>Nested classes/interfaces inherited from interface org.springframework.data.r2dbc.core.ReactiveUpdateOperation
ReactiveUpdateOperation.ReactiveUpdate, ReactiveUpdateOperation.TerminatingUpdate, ReactiveUpdateOperation.UpdateWithQuery, ReactiveUpdateOperation.UpdateWithTable -
Constructor Summary
ConstructorsConstructorDescriptionR2dbcEntityTemplate(ConnectionFactory connectionFactory) Create a newR2dbcEntityTemplategivenConnectionFactory.R2dbcEntityTemplate(DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy) R2dbcEntityTemplate(DatabaseClient databaseClient, R2dbcDialect dialect) Create a newR2dbcEntityTemplategivenDatabaseClient.R2dbcEntityTemplate(DatabaseClient databaseClient, R2dbcDialect dialect, R2dbcConverter converter) -
Method Summary
Modifier and TypeMethodDescriptionReturns the number of rows for the given entity class applyingQuery.Begin creating aDELETEoperation for the givendomainType.Remove entities (rows)/columns from the table byQuery.<T> Mono<T>delete(T entity) Delete the given entity and emit the entity if the delete was applied.Determine whether the result forentityClassQueryyields at least one row.Return the underlyingR2dbcConverter.Expose the underlyingReactiveDataAccessStrategyencapsulating dialect specifics.Expose the underlyingDatabaseClientto allow SQL operations.<T> RowsFetchSpec<T>getRowsFetchSpec(DatabaseClient.GenericExecuteSpec executeSpec, Class<?> entityType, Class<T> resultType) Begin creating anINSERToperation for givendomainType.<T> Mono<T>insert(T entity) Insert the given entity and emit the entity if the insert was applied.protected <T> Mono<T>maybeCallAfterConvert(T object, org.springframework.data.relational.core.sql.SqlIdentifier table) protected <T> Mono<T>maybeCallAfterSave(T object, OutboundRow row, org.springframework.data.relational.core.sql.SqlIdentifier table) protected <T> Mono<T>maybeCallBeforeConvert(T object, org.springframework.data.relational.core.sql.SqlIdentifier table) protected <T> Mono<T>maybeCallBeforeSave(T object, OutboundRow row, org.springframework.data.relational.core.sql.SqlIdentifier table) <T> RowsFetchSpec<T>query(PreparedOperation<?> operation, Class<?> entityClass, Class<T> resultType) Execute a query for aRowsFetchSpec, givenPreparedOperation.<T> RowsFetchSpec<T>query(PreparedOperation<?> operation, Class<?> entityClass, BiFunction<Row, RowMetadata, T> rowMapper) <T> RowsFetchSpec<T>query(PreparedOperation<?> operation, Class<T> entityClass) Execute a query for aRowsFetchSpec, givenPreparedOperation.<T> RowsFetchSpec<T>query(PreparedOperation<?> operation, BiFunction<Row, RowMetadata, T> rowMapper) Execute a query for aRowsFetchSpec, givenPreparedOperation.Begin creating aSELECToperation for the givendomainType.<T> Flux<T>Execute aSELECTquery and convert the resulting items to a stream of entities.<T> Mono<T>Execute aSELECTquery and convert the resulting item to an entity ensuring exactly one result.voidsetApplicationContext(ApplicationContext applicationContext) voidsetBeanFactory(BeanFactory beanFactory) Deprecated.voidsetEntityCallbacks(ReactiveEntityCallbacks entityCallbacks) Set theReactiveEntityCallbacksinstance to use when invokingcallbackslike theBeforeSaveCallback.voidsetStatementFilterFunction(Function<Statement, Statement> statementFilterFunction) Set aStatement Filter Functionthat is applied to everyStatement.Begin creating anUPDATEoperation for the givendomainType.update(org.springframework.data.relational.core.query.Query query, org.springframework.data.relational.core.query.Update update, Class<?> entityClass) Update the queried entities and return true if the update was applied.<T> Mono<T>update(T entity) Update the given entity and emit the entity if the update was applied.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.data.r2dbc.core.R2dbcEntityOperations
query, query
-
Constructor Details
-
R2dbcEntityTemplate
Create a newR2dbcEntityTemplategivenConnectionFactory.- Parameters:
connectionFactory- must not be null.- Since:
- 1.2
-
R2dbcEntityTemplate
Create a newR2dbcEntityTemplategivenDatabaseClient.- Parameters:
databaseClient- must not be null.dialect- the dialect to use, must not be null.- Since:
- 1.2
-
R2dbcEntityTemplate
public R2dbcEntityTemplate(DatabaseClient databaseClient, R2dbcDialect dialect, R2dbcConverter converter) - 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
- Parameters:
databaseClient- must not be null.- Since:
- 1.2
-
-
Method Details
-
setStatementFilterFunction
Set aStatement Filter Functionthat is applied to everyStatement.- Parameters:
statementFilterFunction- must not be null.- Since:
- 3.4
-
getDatabaseClient
Description copied from interface:R2dbcEntityOperationsExpose the underlyingDatabaseClientto allow SQL operations.- Specified by:
getDatabaseClientin interfaceR2dbcEntityOperations- Returns:
- the underlying
DatabaseClient. - See Also:
-
getDataAccessStrategy
Description copied from interface:R2dbcEntityOperationsExpose the underlyingReactiveDataAccessStrategyencapsulating dialect specifics.- Specified by:
getDataAccessStrategyin interfaceR2dbcEntityOperations- Returns:
- the underlying
ReactiveDataAccessStrategy. - See Also:
-
getConverter
Description copied from interface:R2dbcEntityOperationsReturn the underlyingR2dbcConverter.- Specified by:
getConverterin interfaceR2dbcEntityOperations- Returns:
- the underlying
R2dbcConverter.
-
setBeanFactory
Deprecated.- Specified by:
setBeanFactoryin interfaceBeanFactoryAware- Throws:
BeansException
-
setApplicationContext
- Specified by:
setApplicationContextin interfaceApplicationContextAware- Throws:
BeansException
-
setEntityCallbacks
Set theReactiveEntityCallbacksinstance to use when invokingcallbackslike theBeforeSaveCallback.Overrides potentially existing
ReactiveEntityCallbacks.- Parameters:
entityCallbacks- must not be null.- Throws:
IllegalArgumentException- if the given instance is null.- Since:
- 1.2
-
select
Description copied from interface:ReactiveSelectOperationBegin creating aSELECToperation for the givendomainType.- Specified by:
selectin interfaceReactiveSelectOperation- Type Parameters:
T-typeof the application domain object.- Parameters:
domainType-typeof the domain object to query; must not be null.- Returns:
- new instance of
ReactiveSelectOperation.ReactiveSelect. - See Also:
-
insert
Description copied from interface:ReactiveInsertOperationBegin creating anINSERToperation for givendomainType.- Specified by:
insertin interfaceReactiveInsertOperation- Type Parameters:
T-typeof the application domain object.- Parameters:
domainType-typeof the domain object to insert; must not be null.- Returns:
- new instance of
ReactiveInsertOperation.ReactiveInsert. - See Also:
-
update
Description copied from interface:ReactiveUpdateOperationBegin creating anUPDATEoperation for the givendomainType.- Specified by:
updatein interfaceReactiveUpdateOperation- Parameters:
domainType-typeof domain object to update; must not be null.- Returns:
- new instance of
ReactiveUpdateOperation.ReactiveUpdate. - See Also:
-
delete
Description copied from interface:ReactiveDeleteOperationBegin creating aDELETEoperation for the givendomainType.- Specified by:
deletein interfaceReactiveDeleteOperation- Parameters:
domainType-typeof domain object to delete; must not be null.- Returns:
- new instance of
ReactiveDeleteOperation.ReactiveDelete. - See Also:
-
count
public Mono<Long> count(org.springframework.data.relational.core.query.Query query, Class<?> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperationsReturns the number of rows for the given entity class applyingQuery. This overridden method allows users to further refine the selection Query using aQuerypredicate to determine how many entities of the giventypematch the Query.- Specified by:
countin interfaceR2dbcEntityOperations- Parameters:
query- user-defined countQueryto execute; must not be null.entityClass-typeof the entity; must not be null.- Returns:
- the number of existing entities.
- Throws:
DataAccessException- if any problem occurs while executing the query.
-
exists
public Mono<Boolean> exists(org.springframework.data.relational.core.query.Query query, Class<?> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperationsDetermine whether the result forentityClassQueryyields at least one row.- Specified by:
existsin interfaceR2dbcEntityOperations- Parameters:
query- user-defined existsQueryto execute; must not be null.entityClass-typeof the entity; must not be null.- Returns:
- true if the object exists.
- Throws:
DataAccessException- if any problem occurs while executing the query.
-
select
public <T> Flux<T> select(org.springframework.data.relational.core.query.Query query, Class<T> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperationsExecute aSELECTquery and convert the resulting items to a stream of entities.- Specified by:
selectin interfaceR2dbcEntityOperations- Parameters:
query- must not be null.entityClass- the entity type must not be null.- Returns:
- the result objects returned by the action.
- Throws:
DataAccessException- if there is any problem issuing the execution.
-
selectOne
public <T> Mono<T> selectOne(org.springframework.data.relational.core.query.Query query, Class<T> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperationsExecute aSELECTquery and convert the resulting item to an entity ensuring exactly one result.- Specified by:
selectOnein interfaceR2dbcEntityOperations- 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:
IncorrectResultSizeDataAccessException- if more than one match found.DataAccessException- if there is any problem issuing the execution.
-
update
public Mono<Long> update(org.springframework.data.relational.core.query.Query query, org.springframework.data.relational.core.query.Update update, Class<?> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperationsUpdate the queried entities and return true if the update was applied.- Specified by:
updatein interfaceR2dbcEntityOperations- 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:
DataAccessException- if there is any problem executing the query.
-
delete
public Mono<Long> delete(org.springframework.data.relational.core.query.Query query, Class<?> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperationsRemove entities (rows)/columns from the table byQuery.- Specified by:
deletein interfaceR2dbcEntityOperations- Parameters:
query- must not be null.entityClass- the entity type must not be null.- Returns:
- the number of affected rows.
- Throws:
DataAccessException- if there is any problem issuing the execution.
-
query
Description copied from interface:R2dbcEntityOperationsExecute a query for aRowsFetchSpec, givenPreparedOperation. Any provided bindings withinPreparedOperationare applied to the underlyingDatabaseClient. The query is issued as-is without additional pre-processing such as named parameter expansion. Results of the query are mapped ontoentityClass.- Specified by:
queryin interfaceR2dbcEntityOperations- Parameters:
operation- the prepared operation wrapping a SQL query and bind parameters.entityClass- the entity type must not be null.- Returns:
- a
RowsFetchSpecready to materialize.
-
query
public <T> RowsFetchSpec<T> query(PreparedOperation<?> operation, Class<?> entityClass, Class<T> resultType) throws DataAccessException Description copied from interface:R2dbcEntityOperationsExecute a query for aRowsFetchSpec, givenPreparedOperation. Any provided bindings withinPreparedOperationare applied to the underlyingDatabaseClient. The query is issued as-is without additional pre-processing such as named parameter expansion. Results of the query are mapped ontoentityClass.- Specified by:
queryin interfaceR2dbcEntityOperations- Parameters:
operation- the prepared operation wrapping a SQL query and bind parameters.entityClass- the entity type must not be null.resultType- the returned entity, type must not be null.- Returns:
- a
RowsFetchSpecready to materialize. - Throws:
DataAccessException- if there is any problem issuing the execution.
-
query
public <T> RowsFetchSpec<T> query(PreparedOperation<?> operation, BiFunction<Row, RowMetadata, T> rowMapper) Description copied from interface:R2dbcEntityOperationsExecute a query for aRowsFetchSpec, givenPreparedOperation. Any provided bindings withinPreparedOperationare applied to the underlyingDatabaseClient. The query is issued as-is without additional pre-processing such as named parameter expansion. Results of the query are mapped usingrowMapper.- Specified by:
queryin interfaceR2dbcEntityOperations- Parameters:
operation- the prepared operation wrapping a SQL query and bind parameters.rowMapper- the row mapper must not be null.- Returns:
- a
RowsFetchSpecwithrowMapperapplied ready to materialize.
-
query
public <T> RowsFetchSpec<T> query(PreparedOperation<?> operation, Class<?> entityClass, BiFunction<Row, RowMetadata, T> rowMapper) Description copied from interface:R2dbcEntityOperationsExecute a query for aRowsFetchSpecin the context ofentityClass, givenPreparedOperation. Any provided bindings withinPreparedOperationare applied to the underlyingDatabaseClient. The query is issued as-is without additional pre-processing such as named parameter expansion. Results of the query are mapped usingrowMapper.- Specified by:
queryin interfaceR2dbcEntityOperations- 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
RowsFetchSpecwithrowMapperapplied ready to materialize. - See Also:
-
insert
Description copied from interface:R2dbcEntityOperationsInsert the given entity and emit the entity if the insert was applied.- Specified by:
insertin interfaceR2dbcEntityOperations- Parameters:
entity- the entity to insert, must not be null.- Returns:
- the inserted entity.
- Throws:
DataAccessException- if there is any problem issuing the execution.
-
update
Description copied from interface:R2dbcEntityOperationsUpdate the given entity and emit the entity if the update was applied.- Specified by:
updatein interfaceR2dbcEntityOperations- Parameters:
entity- the entity to update, must not be null.- Returns:
- the updated entity.
- Throws:
DataAccessException- if there is any problem issuing the execution.TransientDataAccessResourceException- if the update did not affect any rows.
-
delete
Description copied from interface:R2dbcEntityOperationsDelete the given entity and emit the entity if the delete was applied.- Specified by:
deletein interfaceR2dbcEntityOperations- Parameters:
entity- must not be null.- Returns:
- the deleted entity.
- Throws:
DataAccessException- if there is any problem issuing the execution.
-
maybeCallBeforeConvert
protected <T> Mono<T> maybeCallBeforeConvert(T object, org.springframework.data.relational.core.sql.SqlIdentifier table) -
maybeCallBeforeSave
protected <T> Mono<T> maybeCallBeforeSave(T object, OutboundRow row, org.springframework.data.relational.core.sql.SqlIdentifier table) -
maybeCallAfterSave
protected <T> Mono<T> maybeCallAfterSave(T object, OutboundRow row, org.springframework.data.relational.core.sql.SqlIdentifier table) -
maybeCallAfterConvert
protected <T> Mono<T> maybeCallAfterConvert(T object, org.springframework.data.relational.core.sql.SqlIdentifier table) -
getRowsFetchSpec
public <T> RowsFetchSpec<T> getRowsFetchSpec(DatabaseClient.GenericExecuteSpec executeSpec, Class<?> entityType, Class<T> resultType) - Specified by:
getRowsFetchSpecin interfaceR2dbcEntityOperations
-