diff --git a/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java b/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java index e5ff6f99a..60d523e86 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java +++ b/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java @@ -23,7 +23,6 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Optional; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; @@ -219,10 +218,10 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter rdo.setId(converter.getConversionService().convert(id, String.class)); if (!(item instanceof RedisData)) { - KeyValuePersistentProperty idProperty = converter.getMappingContext().getPersistentEntity(item.getClass()).get() - .getIdProperty().get(); - converter.getMappingContext().getPersistentEntity(item.getClass()).get().getPropertyAccessor(item) - .setProperty(idProperty, Optional.ofNullable(id)); + KeyValuePersistentProperty idProperty = converter.getMappingContext() + .getRequiredPersistentEntity(item.getClass()).getRequiredIdProperty(); + converter.getMappingContext().getRequiredPersistentEntity(item.getClass()).getPropertyAccessor(item) + .setProperty(idProperty, id); } } @@ -441,8 +440,8 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter public void update(final PartialUpdate update) { - final RedisPersistentEntity entity = this.converter.getMappingContext().getPersistentEntity(update.getTarget()) - .get(); + final RedisPersistentEntity entity = this.converter.getMappingContext() + .getRequiredPersistentEntity(update.getTarget()); final String keyspace = entity.getKeySpace(); final Object id = update.getId(); @@ -632,32 +631,28 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter return target; } - RedisPersistentEntity entity = this.converter.getMappingContext().getPersistentEntity(target.getClass()).get(); + RedisPersistentEntity entity = this.converter.getMappingContext().getRequiredPersistentEntity(target.getClass()); if (entity.hasExplictTimeToLiveProperty()) { - Optional ttlProperty = entity.getExplicitTimeToLiveProperty(); - if (!ttlProperty.isPresent()) { + RedisPersistentProperty ttlProperty = entity.getExplicitTimeToLiveProperty(); + if (ttlProperty == null) { return target; } - final Optional ttl = ttlProperty.get().findAnnotation(TimeToLive.class); + TimeToLive ttl = ttlProperty.findAnnotation(TimeToLive.class); - Long timeout = redisOps.execute(new RedisCallback() { + Long timeout = redisOps.execute((RedisCallback) connection -> { - @Override - public Long doInRedis(RedisConnection connection) throws DataAccessException { - - if (ObjectUtils.nullSafeEquals(TimeUnit.SECONDS, ttl.get().unit())) { - return connection.ttl(key); - } - - return connection.pTtl(key, ttl.get().unit()); + if (ObjectUtils.nullSafeEquals(TimeUnit.SECONDS, ttl.unit())) { + return connection.ttl(key); } + + return connection.pTtl(key, ttl.unit()); }); - if (timeout != null || !ttlProperty.get().getType().isPrimitive()) { - entity.getPropertyAccessor(target).setProperty(ttlProperty.get(), - Optional.ofNullable(converter.getConversionService().convert(timeout, ttlProperty.get().getType()))); + if (timeout != null || !ttlProperty.getType().isPrimitive()) { + entity.getPropertyAccessor(target).setProperty(ttlProperty, + converter.getConversionService().convert(timeout, ttlProperty.getType())); } } @@ -757,7 +752,7 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter } /** - * {@link MessageListener} implementation used to capture Redis keypspace notifications. Tries to read a previously + * {@link MessageListener} implementation used to capture Redis keyspace notifications. Tries to read a previously * created phantom key {@code keyspace:id:phantom} to provide the expired object as part of the published * {@link RedisKeyExpiredEvent}. * diff --git a/src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java b/src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java index 9d4de2e1d..df94d57aa 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java +++ b/src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java @@ -16,8 +16,17 @@ package org.springframework.data.redis.core.convert; import java.lang.reflect.Array; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -37,11 +46,11 @@ import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver; import org.springframework.data.mapping.Alias; import org.springframework.data.mapping.Association; import org.springframework.data.mapping.AssociationHandler; +import org.springframework.data.mapping.MappingException; import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PreferredConstructor; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.mapping.context.PersistentPropertyPath; -import org.springframework.data.mapping.model.MappingException; import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider; import org.springframework.data.mapping.model.PropertyValueProvider; import org.springframework.data.redis.core.PartialUpdate; @@ -169,8 +178,12 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { return null; } - TypeInformation readType = typeMapper.readType(source).orElse(ClassTypeInformation.from(type)); - final Optional> entity = mappingContext.getPersistentEntity(readType); + TypeInformation readType = typeMapper.readType(source); + if (readType == null) { + readType = ClassTypeInformation.from(type); + } + + RedisPersistentEntity entity = mappingContext.getPersistentEntity(readType); if (customConversions.hasCustomReadTarget(Map.class, readType.getType())) { @@ -187,9 +200,8 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { } R instance = (R) conversionService.convert(partial, readType.getType()); - if (entity.isPresent() && entity.get().hasIdProperty()) { - entity.get().getPropertyAccessor(instance).setProperty(entity.get().getIdProperty().get(), - Optional.ofNullable(source.getId())); + if (entity != null && entity.hasIdProperty()) { + entity.getPropertyAccessor(instance).setProperty(entity.getRequiredIdProperty(), source.getId()); } return instance; } @@ -199,23 +211,22 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { readType.getType()); } - EntityInstantiator instantiator = entityInstantiators.getInstantiatorFor(entity.get()); + EntityInstantiator instantiator = entityInstantiators.getInstantiatorFor(entity); - Object instance = instantiator.createInstance((RedisPersistentEntity) entity.get(), - new PersistentEntityParameterValueProvider(entity.get(), - new ConverterAwareParameterValueProvider(path, source, conversionService), - Optional.of(this.conversionService))); + Object instance = instantiator.createInstance((RedisPersistentEntity) entity, + new PersistentEntityParameterValueProvider<>(entity, + new ConverterAwareParameterValueProvider(path, source, conversionService), this.conversionService)); - final PersistentPropertyAccessor accessor = entity.get().getPropertyAccessor(instance); + final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(instance); - entity.get().doWithProperties(new PropertyHandler() { + entity.doWithProperties(new PropertyHandler() { @Override public void doWithPersistentProperty(RedisPersistentProperty persistentProperty) { String currentPath = !path.isEmpty() ? path + "." + persistentProperty.getName() : persistentProperty.getName(); - PreferredConstructor constructor = entity.get().getPersistenceConstructor().get(); + PreferredConstructor constructor = entity.getPersistenceConstructor(); if (constructor.isConstructorParameter(persistentProperty)) { return; @@ -224,29 +235,32 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { if (persistentProperty.isMap()) { Map targetValue = null; - Class mapValueType = persistentProperty.getMapValueType() - .orElseThrow(() -> new IllegalArgumentException("Unable to retrieve MapValueType!")); + Class mapValueType = persistentProperty.getMapValueType(); + + if (mapValueType == null) { + throw new IllegalArgumentException("Unable to retrieve MapValueType!"); + } if (conversionService.canConvert(byte[].class, mapValueType)) { targetValue = readMapOfSimpleTypes(currentPath, persistentProperty.getType(), - persistentProperty.getComponentType().get(), mapValueType, source); + persistentProperty.getComponentType(), mapValueType, source); } else { targetValue = readMapOfComplexTypes(currentPath, persistentProperty.getType(), - persistentProperty.getComponentType().get(), mapValueType, source); + persistentProperty.getComponentType(), mapValueType, source); } if (targetValue != null) { - accessor.setProperty(persistentProperty, Optional.ofNullable(targetValue)); + accessor.setProperty(persistentProperty, targetValue); } } else if (persistentProperty.isCollectionLike()) { Object targetValue = readCollectionOrArray(currentPath, persistentProperty.getType(), - persistentProperty.getTypeInformation().getComponentType().get().getActualType().getType(), + persistentProperty.getTypeInformation().getRequiredComponentType().getActualType().getType(), source.getBucket()); if (targetValue != null) { - accessor.setProperty(persistentProperty, Optional.ofNullable(targetValue)); + accessor.setProperty(persistentProperty, targetValue); } } else if (persistentProperty.isEntity() && !conversionService.canConvert(byte[].class, @@ -265,28 +279,27 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { R val = readInternal(currentPath, targetType, newBucket); - accessor.setProperty(persistentProperty, Optional.ofNullable(val)); + accessor.setProperty(persistentProperty, val); } else { if (persistentProperty.isIdProperty() && StringUtils.isEmpty(path.isEmpty())) { if (source.getBucket().get(currentPath) == null) { accessor.setProperty(persistentProperty, - Optional.of(fromBytes(source.getBucket().get(currentPath), persistentProperty.getActualType()))); + fromBytes(source.getBucket().get(currentPath), persistentProperty.getActualType())); } else { - accessor.setProperty(persistentProperty, Optional.ofNullable(source.getId())); + accessor.setProperty(persistentProperty, source.getId()); } } Class typeToUse = getTypeHint(currentPath, source.getBucket(), persistentProperty.getActualType()); - accessor.setProperty(persistentProperty, - Optional.ofNullable(fromBytes(source.getBucket().get(currentPath), typeToUse))); + accessor.setProperty(persistentProperty, fromBytes(source.getBucket().get(currentPath), typeToUse)); } } }); - readAssociation(path, source, entity.get(), accessor); + readAssociation(path, source, entity, accessor); return (R) instance; } @@ -307,7 +320,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { Bucket bucket = source.getBucket().extract(currentPath + ".["); Collection target = CollectionFactory.createCollection(association.getInverse().getType(), - association.getInverse().getComponentType().orElse(Object.class), bucket.size()); + association.getInverse().getComponentType(), bucket.size()); for (Entry entry : bucket.entrySet()) { @@ -321,7 +334,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { } } - accessor.setProperty(association.getInverse(), Optional.ofNullable(target)); + accessor.setProperty(association.getInverse(), target); } else { @@ -338,7 +351,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { if (!CollectionUtils.isEmpty(rawHash)) { accessor.setProperty(association.getInverse(), - Optional.ofNullable(read(association.getInverse().getActualType(), new RedisData(rawHash)))); + read(association.getInverse().getActualType(), new RedisData(rawHash))); } } } @@ -362,42 +375,42 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { return; } - final Optional> entity = mappingContext.getPersistentEntity(source.getClass()); + RedisPersistentEntity entity = mappingContext.getPersistentEntity(source.getClass()); if (!customConversions.hasCustomWriteTarget(source.getClass())) { typeMapper.writeType(ClassUtils.getUserClass(source), sink); } - if (!entity.isPresent()) { + if (entity == null) { typeMapper.writeType(ClassUtils.getUserClass(source), sink); sink.getBucket().put("_raw", conversionService.convert(source, byte[].class)); return; } - sink.setKeyspace(entity.get().getKeySpace()); + sink.setKeyspace(entity.getKeySpace()); - writeInternal(entity.get().getKeySpace(), "", source, entity.get().getTypeInformation(), sink); + writeInternal(entity.getKeySpace(), "", source, entity.getTypeInformation(), sink); - Optional identifyer = entity.get().getIdentifierAccessor(source).getIdentifier(); + Object identifier = entity.getIdentifierAccessor(source).getIdentifier(); - if (identifyer.isPresent()) { - sink.setId(getConversionService().convert(identifyer.get(), String.class)); + if (identifier != null) { + sink.setId(getConversionService().convert(identifier, String.class)); } - Long ttl = entity.get().getTimeToLiveAccessor().getTimeToLive(source); + Long ttl = entity.getTimeToLiveAccessor().getTimeToLive(source); if (ttl != null && ttl > 0) { sink.setTimeToLive(ttl); } - for (IndexedData indexedData : indexResolver.resolveIndexesFor(entity.get().getTypeInformation(), source)) { + for (IndexedData indexedData : indexResolver.resolveIndexesFor(entity.getTypeInformation(), source)) { sink.addIndexedData(indexedData); } } protected void writePartialUpdate(PartialUpdate update, RedisData sink) { - RedisPersistentEntity entity = mappingContext.getPersistentEntity(update.getTarget()).get(); + RedisPersistentEntity entity = mappingContext.getRequiredPersistentEntity(update.getTarget()); write(update.getValue(), sink); if (sink.getBucket().keySet().contains(TYPE_HINT_ALIAS)) { @@ -441,7 +454,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { TypeInformation ti = targetProperty == null ? ClassTypeInformation.OBJECT : (targetProperty.isMap() ? (targetProperty.getTypeInformation().getMapValueType() != null - ? targetProperty.getTypeInformation().getMapValueType().get() : ClassTypeInformation.OBJECT) + ? targetProperty.getTypeInformation().getRequiredMapValueType() : ClassTypeInformation.OBJECT) : targetProperty.getTypeInformation().getActualType()); writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), ti, sink); @@ -452,27 +465,26 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { if (targetProperty.isCollectionLike()) { - RedisPersistentEntity ref = mappingContext.getPersistentEntity(targetProperty.getAssociation().get() - .getInverse().getTypeInformation().getComponentType().get().getActualType()).get(); + RedisPersistentEntity ref = mappingContext.getPersistentEntity(targetProperty.getRequiredAssociation() + .getInverse().getTypeInformation().getRequiredComponentType().getActualType()); int i = 0; for (Object o : (Collection) pUpdate.getValue()) { - Optional refId = ref.getPropertyAccessor(o).getProperty(ref.getIdProperty().get()); - if (refId.isPresent()) { - sink.getBucket().put(pUpdate.getPropertyPath() + ".[" + i + "]", - toBytes(ref.getKeySpace() + ":" + refId.get())); + Object refId = ref.getPropertyAccessor(o).getProperty(ref.getRequiredIdProperty()); + if (refId != null) { + sink.getBucket().put(pUpdate.getPropertyPath() + ".[" + i + "]", toBytes(ref.getKeySpace() + ":" + refId)); i++; } } } else { RedisPersistentEntity ref = mappingContext - .getPersistentEntity(targetProperty.getAssociation().get().getInverse().getTypeInformation()).get(); + .getRequiredPersistentEntity(targetProperty.getRequiredAssociation().getInverse().getTypeInformation()); - Optional refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getIdProperty().get()); - if (refId.isPresent()) { - sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId.get())); + Object refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getRequiredIdProperty()); + if (refId != null) { + sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId)); } } } else if (targetProperty.isCollectionLike()) { @@ -495,8 +507,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { pUpdate.getPropertyPath(), pUpdate.getValue())); } - writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType().orElse(Object.class), - map, sink); + writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType(), map, sink); } else { writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(), @@ -563,7 +574,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { toBytes(value.getClass().getName())); } - final RedisPersistentEntity entity = mappingContext.getPersistentEntity(value.getClass()).get(); + final RedisPersistentEntity entity = mappingContext.getRequiredPersistentEntity(value.getClass()); final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value); entity.doWithProperties(new PropertyHandler() { @@ -573,37 +584,36 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { String propertyStringPath = (!path.isEmpty() ? path + "." : "") + persistentProperty.getName(); + Object propertyValue = accessor.getProperty(persistentProperty); if (persistentProperty.isIdProperty()) { - if (accessor.getProperty(persistentProperty).isPresent()) { - sink.getBucket().put(propertyStringPath, toBytes(accessor.getProperty(persistentProperty).get())); + if (propertyValue != null) { + sink.getBucket().put(propertyStringPath, toBytes(propertyValue)); } return; } if (persistentProperty.isMap()) { - if (accessor.getProperty(persistentProperty).isPresent()) { - writeMap(keyspace, propertyStringPath, persistentProperty.getMapValueType().orElse(Object.class), - (Map) accessor.getProperty(persistentProperty).get(), sink); + if (propertyValue != null) { + writeMap(keyspace, propertyStringPath, persistentProperty.getMapValueType(), (Map) propertyValue, + sink); } } else if (persistentProperty.isCollectionLike()) { - final Optional propertyValue = accessor.getProperty(persistentProperty); - - if (!propertyValue.isPresent()) { + if (propertyValue == null) { writeCollection(keyspace, propertyStringPath, (Iterable) null, - persistentProperty.getTypeInformation().getComponentType().get(), sink); + persistentProperty.getTypeInformation().getRequiredComponentType(), sink); } else { - if (Iterable.class.isAssignableFrom(propertyValue.get().getClass())) { + if (Iterable.class.isAssignableFrom(propertyValue.getClass())) { - writeCollection(keyspace, propertyStringPath, (Iterable) propertyValue.get(), - persistentProperty.getTypeInformation().getComponentType().get(), sink); - } else if (propertyValue.isPresent() && propertyValue.get().getClass().isArray()) { + writeCollection(keyspace, propertyStringPath, (Iterable) propertyValue, + persistentProperty.getTypeInformation().getRequiredComponentType(), sink); + } else if (propertyValue.getClass().isArray()) { - writeCollection(keyspace, propertyStringPath, CollectionUtils.arrayToList(propertyValue.get()), - persistentProperty.getTypeInformation().getComponentType().get(), sink); + writeCollection(keyspace, propertyStringPath, CollectionUtils.arrayToList(propertyValue), + persistentProperty.getTypeInformation().getRequiredComponentType(), sink); } else { throw new RuntimeException("Don't know how to handle " + propertyValue.getClass() + " type collection"); @@ -612,16 +622,14 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { } else if (persistentProperty.isEntity()) { - Optional propertyValue = accessor.getProperty(persistentProperty); - if (propertyValue.isPresent()) { - writeInternal(keyspace, propertyStringPath, propertyValue.get(), + if (propertyValue != null) { + writeInternal(keyspace, propertyStringPath, propertyValue, persistentProperty.getTypeInformation().getActualType(), sink); } } else { - Optional propertyValue = accessor.getProperty(persistentProperty); - if (propertyValue.isPresent()) { - writeToBucket(propertyStringPath, propertyValue.get(), sink, persistentProperty.getType()); + if (propertyValue != null) { + writeToBucket(propertyStringPath, propertyValue, sink, persistentProperty.getType()); } } } @@ -644,25 +652,25 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { @Override public void doWithAssociation(Association association) { - Optional refObject = accessor.getProperty(association.getInverse()); - if (!refObject.isPresent()) { + Object refObject = accessor.getProperty(association.getInverse()); + if (refObject == null) { return; } if (association.getInverse().isCollectionLike()) { - RedisPersistentEntity ref = mappingContext.getPersistentEntity( - association.getInverse().getTypeInformation().getComponentType().get().getActualType()).get(); + RedisPersistentEntity ref = mappingContext.getRequiredPersistentEntity( + association.getInverse().getTypeInformation().getRequiredComponentType().getActualType()); String keyspace = ref.getKeySpace(); String propertyStringPath = (!path.isEmpty() ? path + "." : "") + association.getInverse().getName(); int i = 0; - for (Object o : (Collection) refObject.get()) { + for (Object o : (Collection) refObject) { - Optional refId = ref.getPropertyAccessor(o).getProperty(ref.getIdProperty().get()); - if (refId.isPresent()) { - sink.getBucket().put(propertyStringPath + ".[" + i + "]", toBytes(keyspace + ":" + refId.get())); + Object refId = ref.getPropertyAccessor(o).getProperty(ref.getRequiredIdProperty()); + if (refId != null) { + sink.getBucket().put(propertyStringPath + ".[" + i + "]", toBytes(keyspace + ":" + refId)); i++; } } @@ -670,14 +678,14 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { } else { RedisPersistentEntity ref = mappingContext - .getPersistentEntity(association.getInverse().getTypeInformation()).get(); + .getRequiredPersistentEntity(association.getInverse().getTypeInformation()); String keyspace = ref.getKeySpace(); - Optional refId = ref.getPropertyAccessor(refObject.get()).getProperty(ref.getIdProperty().get()); + Object refId = ref.getPropertyAccessor(refObject).getProperty(ref.getIdProperty()); - if (refId.isPresent()) { + if (refId != null) { String propertyStringPath = (!path.isEmpty() ? path + "." : "") + association.getInverse().getName(); - sink.getBucket().put(propertyStringPath, toBytes(keyspace + ":" + refId.get())); + sink.getBucket().put(propertyStringPath, toBytes(keyspace + ":" + refId)); } } } @@ -732,8 +740,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { Optional> targetType = customConversions.getCustomWriteTarget(value.getClass()); if (!targetType.filter(it -> ClassUtils.isAssignable(Map.class, it)).isPresent() - && customConversions.isSimpleType(value.getClass()) - && value.getClass() != propertyType) { + && customConversions.isSimpleType(value.getClass()) && value.getClass() != propertyType) { sink.getBucket().put((!path.isEmpty() ? path + "." + TYPE_HINT_ALIAS : TYPE_HINT_ALIAS), toBytes(value.getClass().getName())); } @@ -1037,10 +1044,10 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { @Override @SuppressWarnings("unchecked") - public Optional getPropertyValue(RedisPersistentProperty property) { + public T getPropertyValue(RedisPersistentProperty property) { String name = StringUtils.hasText(path) ? path + "." + property.getName() : property.getName(); - return Optional.ofNullable((T) conversionService.convert(source.getBucket().get(name), property.getActualType())); + return (T) conversionService.convert(source.getBucket().get(name), property.getActualType()); } } @@ -1065,8 +1072,7 @@ public class MappingRedisConverter implements RedisConverter, InitializingBean { @Override public Alias readAliasFrom(RedisData source) { - return Alias - .ofOptional(Optional.ofNullable(conversionService.convert(source.getBucket().get(typeKey), String.class))); + return Alias.ofNullable(conversionService.convert(source.getBucket().get(typeKey), String.class)); } @Override diff --git a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java index 297b2bbc3..0222c2be0 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/PathIndexResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2016 the original author or authors. + * Copyright 2015-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,6 @@ import java.util.HashSet; import java.util.LinkedHashSet; import java.util.Map; import java.util.Map.Entry; -import java.util.Optional; import java.util.Set; import org.springframework.data.geo.Point; @@ -88,7 +87,7 @@ public class PathIndexResolver implements IndexResolver { * @see org.springframework.data.redis.core.convert.IndexResolver#resolveIndexesFor(org.springframework.data.util.TypeInformation, java.lang.Object) */ public Set resolveIndexesFor(TypeInformation typeInformation, Object value) { - return doResolveIndexesFor(mappingContext.getPersistentEntity(typeInformation).get().getKeySpace(), "", + return doResolveIndexesFor(mappingContext.getRequiredPersistentEntity(typeInformation).getKeySpace(), "", typeInformation, null, value); } @@ -104,78 +103,78 @@ public class PathIndexResolver implements IndexResolver { private Set doResolveIndexesFor(final String keyspace, final String path, TypeInformation typeInformation, PersistentProperty fallback, Object value) { - Optional> entity = mappingContext.getPersistentEntity(typeInformation); + RedisPersistentEntity entity = mappingContext.getPersistentEntity(typeInformation); - if (!entity.isPresent() || (value != null && VALUE_TYPES.contains(value.getClass()))) { + if (entity == null || (value != null && VALUE_TYPES.contains(value.getClass()))) { return resolveIndex(keyspace, path, fallback, value); } // this might happen on update where we address a property within an entity directly - if (!ClassUtils.isAssignable(entity.get().getType(), value.getClass())) { + if (!ClassUtils.isAssignable(entity.getType(), value.getClass())) { String propertyName = path.lastIndexOf('.') > 0 ? path.substring(path.lastIndexOf('.') + 1, path.length()) : path; - return resolveIndex(keyspace, path, entity.get().getPersistentProperty(propertyName).get(), value); + return resolveIndex(keyspace, path, entity.getPersistentProperty(propertyName), value); } - final PersistentPropertyAccessor accessor = entity.get().getPropertyAccessor(value); + final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(value); final Set indexes = new LinkedHashSet(); - entity.get().doWithProperties(new PropertyHandler() { + entity.doWithProperties(new PropertyHandler() { @Override public void doWithPersistentProperty(RedisPersistentProperty persistentProperty) { String currentPath = !path.isEmpty() ? path + "." + persistentProperty.getName() : persistentProperty.getName(); - Optional propertyValue = accessor.getProperty(persistentProperty); + Object propertyValue = accessor.getProperty(persistentProperty); - if (propertyValue.isPresent()) { + if (propertyValue == null) { + return; + } - TypeInformation typeHint = persistentProperty.isMap() - ? persistentProperty.getTypeInformation().getMapValueType().get() - : persistentProperty.getTypeInformation().getActualType(); + TypeInformation typeHint = persistentProperty.isMap() + ? persistentProperty.getTypeInformation().getRequiredMapValueType() + : persistentProperty.getTypeInformation().getActualType(); - if (persistentProperty.isMap()) { + if (persistentProperty.isMap()) { - for (Entry entry : ((Map) propertyValue.get()).entrySet()) { + for (Entry entry : ((Map) propertyValue).entrySet()) { - TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue()); - indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(), - typeToUse.getActualType(), persistentProperty, entry.getValue())); - } - - } else if (persistentProperty.isCollectionLike()) { - - final Iterable iterable; - - if (Iterable.class.isAssignableFrom(propertyValue.get().getClass())) { - iterable = (Iterable) propertyValue.get(); - } else if (propertyValue.get().getClass().isArray()) { - iterable = CollectionUtils.arrayToList(propertyValue.get()); - } else { - throw new RuntimeException( - "Don't know how to handle " + propertyValue.get().getClass() + " type of collection"); - } - - for (Object listValue : iterable) { - - if (listValue != null) { - TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, listValue); - indexes.addAll(doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, - listValue)); - } - } + TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, entry.getValue()); + indexes.addAll(doResolveIndexesFor(keyspace, currentPath + "." + entry.getKey(), typeToUse.getActualType(), + persistentProperty, entry.getValue())); } - else if (persistentProperty.isEntity() - || persistentProperty.getTypeInformation().getActualType().equals(ClassTypeInformation.OBJECT)) { + } else if (persistentProperty.isCollectionLike()) { - typeHint = updateTypeHintForActualValue(typeHint, propertyValue.get()); - indexes.addAll(doResolveIndexesFor(keyspace, currentPath, typeHint.getActualType(), persistentProperty, - propertyValue.get())); + final Iterable iterable; + + if (Iterable.class.isAssignableFrom(propertyValue.getClass())) { + iterable = (Iterable) propertyValue; + } else if (propertyValue.getClass().isArray()) { + iterable = CollectionUtils.arrayToList(propertyValue); } else { - indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, propertyValue.get())); + throw new RuntimeException("Don't know how to handle " + propertyValue.getClass() + " type of collection"); } + + for (Object listValue : iterable) { + + if (listValue != null) { + TypeInformation typeToUse = updateTypeHintForActualValue(typeHint, listValue); + indexes.addAll( + doResolveIndexesFor(keyspace, currentPath, typeToUse.getActualType(), persistentProperty, listValue)); + } + } + } + + else if (persistentProperty.isEntity() + || persistentProperty.getTypeInformation().getActualType().equals(ClassTypeInformation.OBJECT)) { + + typeHint = updateTypeHintForActualValue(typeHint, propertyValue); + indexes.addAll( + doResolveIndexesFor(keyspace, currentPath, typeHint.getActualType(), persistentProperty, propertyValue)); + } else { + indexes.addAll(resolveIndex(keyspace, currentPath, persistentProperty, propertyValue)); } } @@ -184,7 +183,7 @@ public class PathIndexResolver implements IndexResolver { if (typeHint.equals(ClassTypeInformation.OBJECT) || typeHint.getClass().isInterface()) { try { - typeHint = mappingContext.getPersistentEntity(propertyValue.getClass()).get().getTypeInformation(); + typeHint = mappingContext.getRequiredPersistentEntity(propertyValue.getClass()).getTypeInformation(); } catch (Exception e) { // ignore for cases where property value cannot be resolved as an entity, in that case the provided type // hint has to be sufficient diff --git a/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java b/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java index 53a8d42b7..d9c9a8f35 100644 --- a/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java +++ b/src/main/java/org/springframework/data/redis/core/convert/SpelIndexResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; -import java.util.Optional; import java.util.Set; import org.springframework.context.expression.BeanFactoryResolver; @@ -86,13 +85,13 @@ public class SpelIndexResolver implements IndexResolver { return Collections.emptySet(); } - Optional> entity = mappingContext.getPersistentEntity(typeInformation); + RedisPersistentEntity entity = mappingContext.getPersistentEntity(typeInformation); - if (!entity.isPresent()) { + if (entity == null) { return Collections.emptySet(); } - String keyspace = entity.get().getKeySpace(); + String keyspace = entity.getKeySpace(); Set indexes = new HashSet(); diff --git a/src/main/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntity.java b/src/main/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntity.java index 526510725..59f8aed32 100644 --- a/src/main/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntity.java +++ b/src/main/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntity.java @@ -15,12 +15,10 @@ */ package org.springframework.data.redis.core.mapping; -import java.util.Optional; - import org.springframework.data.annotation.Id; import org.springframework.data.keyvalue.core.mapping.BasicKeyValuePersistentEntity; import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver; -import org.springframework.data.mapping.model.MappingException; +import org.springframework.data.mapping.MappingException; import org.springframework.data.redis.core.TimeToLive; import org.springframework.data.redis.core.TimeToLiveAccessor; import org.springframework.data.util.TypeInformation; @@ -28,8 +26,9 @@ import org.springframework.util.Assert; /** * {@link RedisPersistentEntity} implementation. - * + * * @author Christoph Strobl + * @author Mark Paluch * @param */ public class BasicRedisPersistentEntity extends BasicKeyValuePersistentEntity @@ -39,10 +38,10 @@ public class BasicRedisPersistentEntity extends BasicKeyValuePersistentEntity /** * Creates new {@link BasicRedisPersistentEntity}. - * + * * @param information must not be {@literal null}. * @param fallbackKeySpaceResolver can be {@literal null}. - * @param timeToLiveResolver can be {@literal null}. + * @param timeToLiveAccessor can be {@literal null}. */ public BasicRedisPersistentEntity(TypeInformation information, KeySpaceResolver fallbackKeySpaceResolver, TimeToLiveAccessor timeToLiveAccessor) { @@ -75,14 +74,14 @@ public class BasicRedisPersistentEntity extends BasicKeyValuePersistentEntity * @see org.springframework.data.redis.core.mapping.RedisPersistentEntity#getExplicitTimeToLiveProperty() */ @Override - public Optional getExplicitTimeToLiveProperty() { + public RedisPersistentProperty getExplicitTimeToLiveProperty() { return this.getPersistentProperty(TimeToLive.class); } /* - * (non-Javadoc) - * @see org.springframework.data.mapping.model.BasicPersistentEntity#returnPropertyIfBetterIdPropertyCandidateOrNull(org.springframework.data.mapping.PersistentProperty) - */ + * (non-Javadoc) + * @see org.springframework.data.mapping.model.BasicPersistentEntity#returnPropertyIfBetterIdPropertyCandidateOrNull(org.springframework.data.mapping.PersistentProperty) + */ @Override protected RedisPersistentProperty returnPropertyIfBetterIdPropertyCandidateOrNull(RedisPersistentProperty property) { @@ -92,29 +91,27 @@ public class BasicRedisPersistentEntity extends BasicKeyValuePersistentEntity return null; } - Optional currentIdProperty = getIdProperty(); - boolean currentIdPropertyIsSet = currentIdProperty.isPresent(); + RedisPersistentProperty currentIdProperty = getIdProperty(); + boolean currentIdPropertyIsSet = currentIdProperty != null; if (!currentIdPropertyIsSet) { return property; } - boolean currentIdPropertyIsExplicit = currentIdProperty.get().isAnnotationPresent(Id.class); + boolean currentIdPropertyIsExplicit = currentIdProperty.isAnnotationPresent(Id.class); boolean newIdPropertyIsExplicit = property.isAnnotationPresent(Id.class); if (currentIdPropertyIsExplicit && newIdPropertyIsExplicit) { throw new MappingException(String.format( "Attempt to add explicit id property %s but already have an property %s registered " + "as explicit id. Check your mapping configuration!", - property.getField(), currentIdProperty.get().getField())); + property.getField(), currentIdProperty.getField())); } if (!currentIdPropertyIsExplicit && !newIdPropertyIsExplicit) { throw new MappingException( - String.format( - "Attempt to add id property %s but already have an property %s registered " - + "as id. Check your mapping configuration!", - property.getField(), currentIdProperty.get().getField())); + String.format("Attempt to add id property %s but already have an property %s registered " + + "as id. Check your mapping configuration!", property.getField(), currentIdProperty.getField())); } if (newIdPropertyIsExplicit) { diff --git a/src/main/java/org/springframework/data/redis/core/mapping/RedisMappingContext.java b/src/main/java/org/springframework/data/redis/core/mapping/RedisMappingContext.java index 83b61d59d..27fbe9fc2 100644 --- a/src/main/java/org/springframework/data/redis/core/mapping/RedisMappingContext.java +++ b/src/main/java/org/springframework/data/redis/core/mapping/RedisMappingContext.java @@ -19,7 +19,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Map; -import java.util.Optional; import java.util.concurrent.TimeUnit; import org.springframework.core.annotation.AnnotationUtils; @@ -224,11 +223,9 @@ public class RedisMappingContext extends KeyValueMappingContext ttlProperty = resolveTtlProperty(type); - if (ttlProperty != null) { + if (ttlProperty != null && ttlProperty.isAnnotationPresent(TimeToLive.class)) { - if (ttlProperty.findAnnotation(TimeToLive.class).isPresent()) { - unit = ttlProperty.findAnnotation(TimeToLive.class).get().unit(); - } + unit = ttlProperty.findAnnotation(TimeToLive.class).unit(); } if (source instanceof PartialUpdate) { @@ -248,11 +245,11 @@ public class RedisMappingContext extends KeyValueMappingContext ttlPropertyValue = entity.getPropertyAccessor(source).getProperty(ttlProperty); - if (ttlPropertyValue.isPresent()) { - return TimeUnit.SECONDS.convert(((Number) ttlPropertyValue.get()).longValue(), unit); + Object ttlPropertyValue = entity.getPropertyAccessor(source).getProperty(ttlProperty); + if (ttlPropertyValue != null) { + return TimeUnit.SECONDS.convert(((Number) ttlPropertyValue).longValue(), unit); } } else { @@ -294,9 +291,9 @@ public class RedisMappingContext extends KeyValueMappingContext hash = mappingContext.getPersistentEntity(type).get().findAnnotation(RedisHash.class); - if (hash.isPresent() && hash.get().timeToLive() > 0) { - defaultTimeout = hash.get().timeToLive(); + RedisHash hash = mappingContext.getRequiredPersistentEntity(type).findAnnotation(RedisHash.class); + if (hash != null && hash.timeToLive() > 0) { + defaultTimeout = hash.timeToLive(); } defaultTimeouts.put(type, defaultTimeout); @@ -310,13 +307,13 @@ public class RedisMappingContext extends KeyValueMappingContext> ttlProperty = entity.getPersistentProperty(TimeToLive.class); + RedisPersistentEntity entity = mappingContext.getRequiredPersistentEntity(type); + PersistentProperty ttlProperty = entity.getPersistentProperty(TimeToLive.class); - if (ttlProperty.isPresent()) { + if (ttlProperty != null) { - timeoutProperties.put(type, ttlProperty.get()); - return ttlProperty.get(); + timeoutProperties.put(type, ttlProperty); + return ttlProperty; } if (keyspaceConfig.hasSettingsFor(type)) { @@ -325,9 +322,9 @@ public class RedisMappingContext extends KeyValueMappingContext * @since 1.7 @@ -33,7 +31,7 @@ public interface RedisPersistentEntity extends KeyValuePersistentEntity extends KeyValuePersistentEntity getExplicitTimeToLiveProperty(); + RedisPersistentProperty getExplicitTimeToLiveProperty(); } diff --git a/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java b/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java index 949c6c4a1..f79931d60 100644 --- a/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java +++ b/src/main/java/org/springframework/data/redis/hash/Jackson2HashMapper.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * Copyright 2016-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,7 +26,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; -import org.springframework.data.mapping.model.MappingException; +import org.springframework.data.mapping.MappingException; import org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -48,8 +48,8 @@ import com.fasterxml.jackson.databind.SerializationFeature; *

* Flattening requires all property names to not interfere with JSON paths. Using dots or brackets in map keys or as * property names is not supported using flattening. The resulting hash cannot be mapped back into an Object. - * * Example + * *

  * 
  * class Person {
diff --git a/src/main/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformation.java b/src/main/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformation.java
index bc927eee5..90794808d 100644
--- a/src/main/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformation.java
+++ b/src/main/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformation.java
@@ -15,7 +15,7 @@
  */
 package org.springframework.data.redis.repository.core;
 
-import org.springframework.data.mapping.model.MappingException;
+import org.springframework.data.mapping.MappingException;
 import org.springframework.data.redis.core.mapping.RedisPersistentEntity;
 import org.springframework.data.repository.core.support.PersistentEntityInformation;
 
@@ -23,8 +23,9 @@ import org.springframework.data.repository.core.support.PersistentEntityInformat
  * {@link RedisEntityInformation} implementation using a {@link RedisPersistentEntity} instance to lookup the necessary
  * information. Can be configured with a custom collection to be returned which will trump the one returned by the
  * {@link RedisPersistentEntity} if given.
- * 
+ *
  * @author Christoph Strobl
+ * @author Mark Paluch
  * @param 
  * @param 
  */
diff --git a/src/main/java/org/springframework/data/redis/repository/support/RedisRepositoryFactory.java b/src/main/java/org/springframework/data/redis/repository/support/RedisRepositoryFactory.java
index 4be3020ba..9a1c0e75d 100644
--- a/src/main/java/org/springframework/data/redis/repository/support/RedisRepositoryFactory.java
+++ b/src/main/java/org/springframework/data/redis/repository/support/RedisRepositoryFactory.java
@@ -29,9 +29,10 @@ import org.springframework.data.repository.query.parser.AbstractQueryCreator;
 /**
  * {@link RepositoryFactorySupport} specific of handing Redis
  * {@link org.springframework.data.keyvalue.repository.KeyValueRepository}.
- * 
+ *
  * @author Christoph Strobl
  * @author Oliver Gierke
+ * @author Mark Paluch
  * @since 1.7
  */
 public class RedisRepositoryFactory extends KeyValueRepositoryFactory {
@@ -78,10 +79,8 @@ public class RedisRepositoryFactory extends KeyValueRepositoryFactory {
 	public  EntityInformation getEntityInformation(Class domainClass) {
 
 		RedisPersistentEntity entity = (RedisPersistentEntity) operations.getMappingContext()
-				.getPersistentEntity(domainClass).get();
-		EntityInformation entityInformation = (EntityInformation) new MappingRedisEntityInformation(
-				entity);
+				.getRequiredPersistentEntity(domainClass);
 
-		return entityInformation;
+		return new MappingRedisEntityInformation<>(entity);
 	}
 }
diff --git a/src/test/java/org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java b/src/test/java/org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java
index f0751ca5a..e2657f656 100644
--- a/src/test/java/org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java
+++ b/src/test/java/org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java
@@ -45,7 +45,7 @@ import org.mockito.junit.MockitoJUnitRunner;
 import org.springframework.core.convert.converter.Converter;
 import org.springframework.data.convert.ReadingConverter;
 import org.springframework.data.convert.WritingConverter;
-import org.springframework.data.mapping.model.MappingException;
+import org.springframework.data.mapping.MappingException;
 import org.springframework.data.redis.core.PartialUpdate;
 import org.springframework.data.redis.core.convert.KeyspaceConfiguration.KeyspaceSettings;
 import org.springframework.data.redis.core.convert.ConversionTestEntities.*;
diff --git a/src/test/java/org/springframework/data/redis/core/convert/PathIndexResolverUnitTests.java b/src/test/java/org/springframework/data/redis/core/convert/PathIndexResolverUnitTests.java
index e7cf95732..f079a8e06 100644
--- a/src/test/java/org/springframework/data/redis/core/convert/PathIndexResolverUnitTests.java
+++ b/src/test/java/org/springframework/data/redis/core/convert/PathIndexResolverUnitTests.java
@@ -28,7 +28,6 @@ import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
 
 import org.hamcrest.core.IsCollectionContaining;
@@ -42,15 +41,7 @@ import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.springframework.data.geo.Point;
 import org.springframework.data.mapping.PersistentProperty;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.Address;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.AddressWithId;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.Item;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.Location;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.Person;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.PersonWithAddressReference;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.Size;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.TaVeren;
-import org.springframework.data.redis.core.convert.ConversionTestEntities.TheWheelOfTime;
+import org.springframework.data.redis.core.convert.ConversionTestEntities.*;
 import org.springframework.data.redis.core.index.GeoIndexed;
 import org.springframework.data.redis.core.index.IndexConfiguration;
 import org.springframework.data.redis.core.index.Indexed;
@@ -253,7 +244,7 @@ public class PathIndexResolverUnitTests {
 	public void resolveIndexShouldReturnDataWhenNoIndexConfiguredButPropertyAnnotated() {
 
 		when(propertyMock.isAnnotationPresent(eq(Indexed.class))).thenReturn(true);
-		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(Optional.of(createIndexedInstance()));
+		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(createIndexedInstance());
 
 		assertThat(resolve("foo", "rand"), notNullValue());
 	}
@@ -263,7 +254,7 @@ public class PathIndexResolverUnitTests {
 
 		when(propertyMock.isCollectionLike()).thenReturn(true);
 		when(propertyMock.isAnnotationPresent(eq(Indexed.class))).thenReturn(true);
-		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(Optional.of(createIndexedInstance()));
+		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(createIndexedInstance());
 
 		IndexedData index = resolve("list.[0].name", "rand");
 
@@ -275,7 +266,7 @@ public class PathIndexResolverUnitTests {
 
 		when(propertyMock.isMap()).thenReturn(true);
 		when(propertyMock.isAnnotationPresent(eq(Indexed.class))).thenReturn(true);
-		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(Optional.of(createIndexedInstance()));
+		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(createIndexedInstance());
 
 		IndexedData index = resolve("map.[foo].name", "rand");
 
@@ -287,7 +278,7 @@ public class PathIndexResolverUnitTests {
 
 		when(propertyMock.isMap()).thenReturn(true);
 		when(propertyMock.isAnnotationPresent(eq(Indexed.class))).thenReturn(true);
-		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(Optional.of(createIndexedInstance()));
+		when(propertyMock.findAnnotation(eq(Indexed.class))).thenReturn(createIndexedInstance());
 
 		IndexedData index = resolve("map.[0].name", "rand");
 
@@ -449,7 +440,7 @@ public class PathIndexResolverUnitTests {
 
 		when(propertyMock.isMap()).thenReturn(true);
 		when(propertyMock.isAnnotationPresent(eq(GeoIndexed.class))).thenReturn(true);
-		when(propertyMock.findAnnotation(eq(GeoIndexed.class))).thenReturn(Optional.of(createGeoIndexedInstance()));
+		when(propertyMock.findAnnotation(eq(GeoIndexed.class))).thenReturn(createGeoIndexedInstance());
 
 		IndexedData index = resolve("location", new Point(1D, 2D));
 
@@ -461,7 +452,7 @@ public class PathIndexResolverUnitTests {
 
 		when(propertyMock.isMap()).thenReturn(true);
 		when(propertyMock.isAnnotationPresent(eq(GeoIndexed.class))).thenReturn(true);
-		when(propertyMock.findAnnotation(eq(GeoIndexed.class))).thenReturn(Optional.of(createGeoIndexedInstance()));
+		when(propertyMock.findAnnotation(eq(GeoIndexed.class))).thenReturn(createGeoIndexedInstance());
 
 		IndexedData index = resolve("property.location", new Point(1D, 2D));
 
diff --git a/src/test/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntityUnitTests.java b/src/test/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntityUnitTests.java
index 117676477..f6b5bb23e 100644
--- a/src/test/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntityUnitTests.java
+++ b/src/test/java/org/springframework/data/redis/core/mapping/BasicRedisPersistentEntityUnitTests.java
@@ -21,7 +21,6 @@ import static org.junit.Assert.*;
 import static org.mockito.Mockito.*;
 
 import java.io.Serializable;
-import java.util.Optional;
 
 import org.junit.Before;
 import org.junit.Rule;
@@ -31,13 +30,14 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 import org.springframework.data.keyvalue.core.mapping.KeySpaceResolver;
-import org.springframework.data.mapping.model.MappingException;
+import org.springframework.data.mapping.MappingException;
 import org.springframework.data.redis.core.TimeToLiveAccessor;
 import org.springframework.data.redis.core.convert.ConversionTestEntities;
 import org.springframework.data.util.TypeInformation;
 
 /**
  * @author Christoph Strobl
+ * @author Mark Paluch
  * @param 
  * @param 
  */
@@ -111,6 +111,6 @@ public class BasicRedisPersistentEntityUnitTests {
 		entity.addPersistentProperty(property1);
 		entity.addPersistentProperty(property2);
 
-		assertThat(entity.getIdProperty(), is(equalTo(Optional.of(property2))));
+		assertThat(entity.getIdProperty(), is(equalTo(property2)));
 	}
 }
diff --git a/src/test/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformationUnitTests.java b/src/test/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformationUnitTests.java
index e1f960e87..314c86772 100644
--- a/src/test/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformationUnitTests.java
+++ b/src/test/java/org/springframework/data/redis/repository/core/MappingRedisEntityInformationUnitTests.java
@@ -23,12 +23,13 @@ import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
-import org.springframework.data.mapping.model.MappingException;
+import org.springframework.data.mapping.MappingException;
 import org.springframework.data.redis.core.convert.ConversionTestEntities;
 import org.springframework.data.redis.core.mapping.RedisPersistentEntity;
 
 /**
  * @author Christoph Strobl
+ * @author Mark Paluch
  */
 @RunWith(MockitoJUnitRunner.class)
 public class MappingRedisEntityInformationUnitTests {