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
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.data.r2dbc.core.ReactiveDeleteOperation
ReactiveDeleteOperation.DeleteWithQuery, ReactiveDeleteOperation.DeleteWithTable, ReactiveDeleteOperation.ReactiveDelete, ReactiveDeleteOperation.TerminatingDelete
Nested 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 newR2dbcEntityTemplate
givenConnectionFactory
.R2dbcEntityTemplate
(DatabaseClient databaseClient, ReactiveDataAccessStrategy strategy) R2dbcEntityTemplate
(DatabaseClient databaseClient, R2dbcDialect dialect) Create a newR2dbcEntityTemplate
givenDatabaseClient
.R2dbcEntityTemplate
(DatabaseClient databaseClient, R2dbcDialect dialect, R2dbcConverter converter) -
Method Summary
Modifier and TypeMethodDescriptionreactor.core.publisher.Mono<Long>
Returns the number of rows for the given entity class applyingQuery
.Begin creating aDELETE
operation for the givendomainType
.reactor.core.publisher.Mono<Long>
Remove entities (rows)/columns from the table byQuery
.<T> reactor.core.publisher.Mono<T>
delete
(T entity) Delete the given entity and emit the entity if the delete was applied.reactor.core.publisher.Mono<Boolean>
Determine whether the result forentityClass
Query
yields at least one row.Return the underlyingR2dbcConverter
.Expose the underlyingReactiveDataAccessStrategy
encapsulating dialect specifics.Expose the underlyingDatabaseClient
to allow SQL operations.<T> RowsFetchSpec<T>
getRowsFetchSpec
(DatabaseClient.GenericExecuteSpec executeSpec, Class<?> entityType, Class<T> resultType) Begin creating anINSERT
operation for givendomainType
.<T> reactor.core.publisher.Mono<T>
insert
(T entity) Insert the given entity and emit the entity if the insert was applied.protected <T> reactor.core.publisher.Mono<T>
maybeCallAfterConvert
(T object, org.springframework.data.relational.core.sql.SqlIdentifier table) protected <T> reactor.core.publisher.Mono<T>
maybeCallAfterSave
(T object, OutboundRow row, org.springframework.data.relational.core.sql.SqlIdentifier table) protected <T> reactor.core.publisher.Mono<T>
maybeCallBeforeConvert
(T object, org.springframework.data.relational.core.sql.SqlIdentifier table) protected <T> reactor.core.publisher.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 aSELECT
operation for the givendomainType
.<T> reactor.core.publisher.Flux<T>
Execute aSELECT
query and convert the resulting items to a stream of entities.<T> reactor.core.publisher.Mono<T>
Execute aSELECT
query and convert the resulting item to an entity ensuring exactly one result.void
setApplicationContext
(ApplicationContext applicationContext) void
setBeanFactory
(BeanFactory beanFactory) Deprecated.void
setEntityCallbacks
(ReactiveEntityCallbacks entityCallbacks) Set theReactiveEntityCallbacks
instance to use when invokingcallbacks
like theBeforeSaveCallback
.Begin creating anUPDATE
operation for the givendomainType
.reactor.core.publisher.Mono<Long>
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> reactor.core.publisher.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, wait
Methods inherited from interface org.springframework.data.r2dbc.core.R2dbcEntityOperations
query, query
-
Constructor Details
-
R2dbcEntityTemplate
Create a newR2dbcEntityTemplate
givenConnectionFactory
.- Parameters:
connectionFactory
- must not be null.- Since:
- 1.2
-
R2dbcEntityTemplate
Create a newR2dbcEntityTemplate
givenDatabaseClient
.- 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
-
getDatabaseClient
Description copied from interface:R2dbcEntityOperations
Expose the underlyingDatabaseClient
to allow SQL operations.- Specified by:
getDatabaseClient
in interfaceR2dbcEntityOperations
- Returns:
- the underlying
DatabaseClient
. - See Also:
-
getDataAccessStrategy
Description copied from interface:R2dbcEntityOperations
Expose the underlyingReactiveDataAccessStrategy
encapsulating dialect specifics.- Specified by:
getDataAccessStrategy
in interfaceR2dbcEntityOperations
- Returns:
- the underlying
ReactiveDataAccessStrategy
. - See Also:
-
getConverter
Description copied from interface:R2dbcEntityOperations
Return the underlyingR2dbcConverter
.- Specified by:
getConverter
in interfaceR2dbcEntityOperations
- Returns:
- the underlying
R2dbcConverter
.
-
setBeanFactory
Deprecated.- Specified by:
setBeanFactory
in interfaceBeanFactoryAware
- Throws:
BeansException
-
setApplicationContext
- Specified by:
setApplicationContext
in interfaceApplicationContextAware
- Throws:
BeansException
-
setEntityCallbacks
Set theReactiveEntityCallbacks
instance to use when invokingcallbacks
like 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:ReactiveSelectOperation
Begin creating aSELECT
operation for the givendomainType
.- Specified by:
select
in interfaceReactiveSelectOperation
- 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
Description copied from interface:ReactiveInsertOperation
Begin creating anINSERT
operation for givendomainType
.- Specified by:
insert
in interfaceReactiveInsertOperation
- 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
Description copied from interface:ReactiveUpdateOperation
Begin creating anUPDATE
operation for the givendomainType
.- Specified by:
update
in interfaceReactiveUpdateOperation
- Parameters:
domainType
-type
of domain object to update; must not be null.- Returns:
- new instance of
ReactiveUpdateOperation.ReactiveUpdate
. - See Also:
-
delete
Description copied from interface:ReactiveDeleteOperation
Begin creating aDELETE
operation for the givendomainType
.- Specified by:
delete
in interfaceReactiveDeleteOperation
- 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(org.springframework.data.relational.core.query.Query query, Class<?> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperations
Returns the number of rows for the given entity class applyingQuery
. This overridden method allows users to further refine the selection Query using aQuery
predicate to determine how many entities of the giventype
match the Query.- Specified by:
count
in interfaceR2dbcEntityOperations
- Parameters:
query
- user-defined countQuery
to execute; must not be null.entityClass
-type
of the entity; must not be null.- Returns:
- the number of existing entities.
- Throws:
DataAccessException
- if any problem occurs while executing the query.
-
exists
public reactor.core.publisher.Mono<Boolean> exists(org.springframework.data.relational.core.query.Query query, Class<?> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperations
Determine whether the result forentityClass
Query
yields at least one row.- Specified by:
exists
in interfaceR2dbcEntityOperations
- Parameters:
query
- user-defined existsQuery
to execute; must not be null.entityClass
-type
of 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> reactor.core.publisher.Flux<T> select(org.springframework.data.relational.core.query.Query query, Class<T> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperations
Execute aSELECT
query and convert the resulting items to a stream of entities.- Specified by:
select
in 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> reactor.core.publisher.Mono<T> selectOne(org.springframework.data.relational.core.query.Query query, Class<T> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperations
Execute aSELECT
query and convert the resulting item to an entity ensuring exactly one result.- Specified by:
selectOne
in 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 reactor.core.publisher.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:R2dbcEntityOperations
Update the queried entities and return true if the update was applied.- Specified by:
update
in 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 reactor.core.publisher.Mono<Long> delete(org.springframework.data.relational.core.query.Query query, Class<?> entityClass) throws DataAccessException Description copied from interface:R2dbcEntityOperations
Remove entities (rows)/columns from the table byQuery
.- Specified by:
delete
in 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:R2dbcEntityOperations
Execute a query for aRowsFetchSpec
, givenPreparedOperation
. Any provided bindings withinPreparedOperation
are 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:
query
in interfaceR2dbcEntityOperations
- 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> RowsFetchSpec<T> query(PreparedOperation<?> operation, Class<?> entityClass, Class<T> resultType) throws DataAccessException Description copied from interface:R2dbcEntityOperations
Execute a query for aRowsFetchSpec
, givenPreparedOperation
. Any provided bindings withinPreparedOperation
are 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:
query
in 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
RowsFetchSpec
ready 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:R2dbcEntityOperations
Execute a query for aRowsFetchSpec
, givenPreparedOperation
. Any provided bindings withinPreparedOperation
are 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:
query
in interfaceR2dbcEntityOperations
- Parameters:
operation
- the prepared operation wrapping a SQL query and bind parameters.rowMapper
- the row mapper must not be null.- Returns:
- a
RowsFetchSpec
withrowMapper
applied ready to materialize.
-
query
public <T> RowsFetchSpec<T> query(PreparedOperation<?> operation, Class<?> entityClass, BiFunction<Row, RowMetadata, T> rowMapper) Description copied from interface:R2dbcEntityOperations
Execute a query for aRowsFetchSpec
in the context ofentityClass
, givenPreparedOperation
. Any provided bindings withinPreparedOperation
are 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:
query
in 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
RowsFetchSpec
withrowMapper
applied ready to materialize. - See Also:
-
insert
Description copied from interface:R2dbcEntityOperations
Insert the given entity and emit the entity if the insert was applied.- Specified by:
insert
in 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:R2dbcEntityOperations
Update the given entity and emit the entity if the update was applied.- Specified by:
update
in 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:R2dbcEntityOperations
Delete the given entity and emit the entity if the delete was applied.- Specified by:
delete
in interfaceR2dbcEntityOperations
- Parameters:
entity
- must not be null.- Returns:
- the deleted entity.
- Throws:
DataAccessException
- if there is any problem issuing the execution.
-
maybeCallBeforeConvert
protected <T> reactor.core.publisher.Mono<T> maybeCallBeforeConvert(T object, org.springframework.data.relational.core.sql.SqlIdentifier table) -
maybeCallBeforeSave
protected <T> reactor.core.publisher.Mono<T> maybeCallBeforeSave(T object, OutboundRow row, org.springframework.data.relational.core.sql.SqlIdentifier table) -
maybeCallAfterSave
protected <T> reactor.core.publisher.Mono<T> maybeCallAfterSave(T object, OutboundRow row, org.springframework.data.relational.core.sql.SqlIdentifier table) -
maybeCallAfterConvert
protected <T> reactor.core.publisher.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:
getRowsFetchSpec
in interfaceR2dbcEntityOperations
-