Class KeyValueTemplate
java.lang.Object
org.springframework.data.keyvalue.core.KeyValueTemplate
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.beans.factory.DisposableBean
,org.springframework.context.ApplicationEventPublisherAware
,KeyValueOperations
public class KeyValueTemplate
extends Object
implements KeyValueOperations, org.springframework.context.ApplicationEventPublisherAware
Basic implementation of
KeyValueOperations
.- Author:
- Christoph Strobl, Oliver Gierke, Thomas Darimont, Mark Paluch, Mert Zeybekler, Adeyemi Abass
-
Constructor Summary
ConstructorDescriptionKeyValueTemplate
(KeyValueAdapter adapter) KeyValueTemplate
(KeyValueAdapter adapter, org.springframework.data.mapping.context.MappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext) KeyValueTemplate
(KeyValueAdapter adapter, org.springframework.data.mapping.context.MappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext, IdentifierGenerator identifierGenerator) -
Method Summary
Modifier and TypeMethodDescriptionlong
Total number of elements with given type available.long
count
(KeyValueQuery<?> query, Class<?> type) Total number of elements matching given query.void
Remove all elements of type.<T> T
Delete item of type with given id.<T> T
delete
(T objectToDelete) void
destroy()
<T> T
execute
(KeyValueCallback<T> action) Execute operation against underlying store.protected <T> T
executeRequired
(KeyValueCallback<T> action) ExecuteKeyValueCallback
and require a non-null return value.boolean
exists
(KeyValueQuery<?> query, Class<?> type) Determine whether result of givenKeyValueQuery
contains at least one element.<T> Iterable<T>
find
(KeyValueQuery<?> query, Class<T> type) Get all elements matching the given query.<T> Iterable<T>
Get all elements of given type.<T> Iterable<T>
Get all elements ordered by sort.<T> Optional<T>
Get element of given type with given id.<T> Iterable<T>
findInRange
(long offset, int rows, Class<T> type) Get all elements in given range.<T> Iterable<T>
findInRange
(long offset, int rows, org.springframework.data.domain.Sort sort, Class<T> type) Get all elements in given range ordered by sort.org.springframework.data.mapping.context.MappingContext<?,
?> <T> T
Add object with given id.<T> T
insert
(T objectToInsert) Add given object.void
setApplicationEventPublisher
(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) void
setEventTypesToPublish
(Set<Class<? extends KeyValueEvent>> eventTypesToPublish) Define the event types to publish viaApplicationEventPublisher
.void
setExceptionTranslator
(org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator) Set thePersistenceExceptionTranslator
used for convertingRuntimeException
.<T> T
<T> T
update
(T objectToUpdate)
-
Constructor Details
-
KeyValueTemplate
- Parameters:
adapter
- must not be null.
-
KeyValueTemplate
public KeyValueTemplate(KeyValueAdapter adapter, org.springframework.data.mapping.context.MappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext) - Parameters:
adapter
- must not be null.mappingContext
- must not be null.
-
KeyValueTemplate
public KeyValueTemplate(KeyValueAdapter adapter, org.springframework.data.mapping.context.MappingContext<? extends KeyValuePersistentEntity<?, ?>, ? extends KeyValuePersistentProperty<?>> mappingContext, IdentifierGenerator identifierGenerator) - Parameters:
adapter
- must not be null.mappingContext
- must not be null.identifierGenerator
- must not be null.- Since:
- 2.4
-
-
Method Details
-
setExceptionTranslator
public void setExceptionTranslator(org.springframework.dao.support.PersistenceExceptionTranslator exceptionTranslator) Set thePersistenceExceptionTranslator
used for convertingRuntimeException
.- Parameters:
exceptionTranslator
- must not be null.
-
setEventTypesToPublish
Define the event types to publish viaApplicationEventPublisher
.- Parameters:
eventTypesToPublish
- use null orCollections.emptySet()
to stop publishing.
-
setApplicationEventPublisher
public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher) - Specified by:
setApplicationEventPublisher
in interfaceorg.springframework.context.ApplicationEventPublisherAware
-
insert
public <T> T insert(T objectToInsert) Description copied from interface:KeyValueOperations
Add given object. Object needs to have id property to which a generated value will be assigned.- Specified by:
insert
in interfaceKeyValueOperations
- Returns:
- the inserted object.
-
insert
Description copied from interface:KeyValueOperations
Add object with given id.- Specified by:
insert
in interfaceKeyValueOperations
- Parameters:
id
- must not be null.objectToInsert
- must not be null.- Returns:
- the inserted object.
-
update
public <T> T update(T objectToUpdate) - Specified by:
update
in interfaceKeyValueOperations
- Parameters:
objectToUpdate
- must not be null.- Returns:
- the updated object.
-
update
- Specified by:
update
in interfaceKeyValueOperations
- Parameters:
id
- must not be null.objectToUpdate
- must not be null.- Returns:
- the updated object.
-
findAll
Description copied from interface:KeyValueOperations
Get all elements of given type. RespectsKeySpace
if present and therefore returns all elements that can be assigned to requested type.- Specified by:
findAll
in interfaceKeyValueOperations
- Parameters:
type
- must not be null.- Returns:
- empty iterable if no elements found.
-
findById
Description copied from interface:KeyValueOperations
Get element of given type with given id. RespectsKeySpace
if present and therefore returns all elements that can be assigned to requested type.- Specified by:
findById
in interfaceKeyValueOperations
- Parameters:
id
- must not be null.type
- must not be null.- Returns:
Optional.empty()
if not found.
-
delete
Description copied from interface:KeyValueOperations
Remove all elements of type. RespectsKeySpace
if present and therefore removes all elements that can be assigned to requested type.- Specified by:
delete
in interfaceKeyValueOperations
- Parameters:
type
- must not be null.
-
delete
public <T> T delete(T objectToDelete) - Specified by:
delete
in interfaceKeyValueOperations
- Parameters:
objectToDelete
- must not be null.- Returns:
-
delete
Description copied from interface:KeyValueOperations
Delete item of type with given id.- Specified by:
delete
in interfaceKeyValueOperations
- Parameters:
id
- must not be null.type
- must not be null.- Returns:
- the deleted item or null if no match found.
-
count
Description copied from interface:KeyValueOperations
Total number of elements with given type available. RespectsKeySpace
if present and therefore counts all elements that can be assigned to requested type.- Specified by:
count
in interfaceKeyValueOperations
- Parameters:
type
- must not be null.- Returns:
-
execute
Description copied from interface:KeyValueOperations
Execute operation against underlying store.- Specified by:
execute
in interfaceKeyValueOperations
- Parameters:
action
- must not be null.- Returns:
-
executeRequired
ExecuteKeyValueCallback
and require a non-null return value.- Type Parameters:
T
-- Parameters:
action
-- Returns:
-
find
Description copied from interface:KeyValueOperations
Get all elements matching the given query.
RespectsKeySpace
if present and therefore returns all elements that can be assigned to requested type..- Specified by:
find
in interfaceKeyValueOperations
- Parameters:
query
- must not be null.type
- must not be null.- Returns:
- empty iterable if no match found.
-
findAll
Description copied from interface:KeyValueOperations
Get all elements ordered by sort. RespectsKeySpace
if present and therefore returns all elements that can be assigned to requested type.- Specified by:
findAll
in interfaceKeyValueOperations
- Parameters:
sort
- must not be null.type
- must not be null.- Returns:
-
findInRange
Description copied from interface:KeyValueOperations
Get all elements in given range. RespectsKeySpace
if present and therefore returns all elements that can be assigned to requested type.- Specified by:
findInRange
in interfaceKeyValueOperations
type
- must not be null.- Returns:
-
findInRange
public <T> Iterable<T> findInRange(long offset, int rows, org.springframework.data.domain.Sort sort, Class<T> type) Description copied from interface:KeyValueOperations
Get all elements in given range ordered by sort. RespectsKeySpace
if present and therefore returns all elements that can be assigned to requested type.- Specified by:
findInRange
in interfaceKeyValueOperations
- Returns:
-
count
Description copied from interface:KeyValueOperations
Total number of elements matching given query. RespectsKeySpace
if present and therefore counts all elements that can be assigned to requested type.- Specified by:
count
in interfaceKeyValueOperations
- Returns:
-
exists
Description copied from interface:KeyValueOperations
Determine whether result of givenKeyValueQuery
contains at least one element.- Specified by:
exists
in interfaceKeyValueOperations
- Returns:
-
getMappingContext
public org.springframework.data.mapping.context.MappingContext<?,?> getMappingContext()- Specified by:
getMappingContext
in interfaceKeyValueOperations
- Returns:
- mapping context in use.
-
getKeyValueAdapter
- Specified by:
getKeyValueAdapter
in interfaceKeyValueOperations
- Returns:
KeyValueAdapter
in use.
-
destroy
- Specified by:
destroy
in interfaceorg.springframework.beans.factory.DisposableBean
- Throws:
Exception
-