diff --git a/pom.xml b/pom.xml index b20e7c9dcc..f8343e7730 100644 --- a/pom.xml +++ b/pom.xml @@ -1,11 +1,13 @@ - + 4.0.0 org.springframework.data spring-data-redis - 2.5.0-SNAPSHOT + 2.5.0-GH-1995-SNAPSHOT Spring Data Redis @@ -16,7 +18,7 @@ - 2.5.0-SNAPSHOT + 2.5.0-GH-1995-SNAPSHOT 4.0.2 1.1 1.9.2 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 22f29d5200..fae957c508 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java +++ b/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java @@ -373,16 +373,21 @@ public T delete(Object id, String keyspace, Class type) { */ @Override public List getAllOf(String keyspace) { - return getAllOf(keyspace, -1, -1); + return getAllOf(keyspace, Object.class, -1, -1); } - public List getAllOf(String keyspace, long offset, int rows) { + @Override + public Iterable getAllOf(String keyspace, Class type) { + return getAllOf(keyspace, type, -1, -1); + } + + public List getAllOf(String keyspace, Class type, long offset, int rows) { byte[] binKeyspace = toBytes(keyspace); Set ids = redisOps.execute((RedisCallback>) connection -> connection.sMembers(binKeyspace)); - List result = new ArrayList<>(); + List result = new ArrayList<>(); List keys = new ArrayList<>(ids); if (keys.isEmpty() || keys.size() < offset) { @@ -395,7 +400,7 @@ public List getAllOf(String keyspace, long offset, int rows) { } for (byte[] key : keys) { - result.add(get(key, keyspace)); + result.add(get(key, keyspace, type)); } return result; } diff --git a/src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java b/src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java index bb900c8e62..358e266b29 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java +++ b/src/main/java/org/springframework/data/redis/core/RedisQueryEngine.java @@ -80,7 +80,7 @@ public Collection execute(RedisOperationChain criteria, Comparator sor if (criteria == null || (CollectionUtils.isEmpty(criteria.getOrSismember()) && CollectionUtils.isEmpty(criteria.getSismember())) && criteria.getNear() == null) { - return (Collection) getAdapter().getAllOf(keyspace, offset, rows); + return getAdapter().getAllOf(keyspace, type, offset, rows); } RedisCallback>> callback = connection -> { diff --git a/src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java b/src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java index 9bc108c962..ff54a39891 100644 --- a/src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java +++ b/src/test/java/org/springframework/data/redis/core/RedisKeyValueAdapterTests.java @@ -34,6 +34,7 @@ import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Reference; +import org.springframework.data.annotation.TypeAlias; import org.springframework.data.geo.Point; import org.springframework.data.keyvalue.annotation.KeySpace; import org.springframework.data.redis.connection.RedisConnection; @@ -62,6 +63,7 @@ public class RedisKeyValueAdapterTests { private RedisKeyValueAdapter adapter; private StringRedisTemplate template; private RedisConnectionFactory connectionFactory; + private RedisMappingContext mappingContext; public RedisKeyValueAdapterTests(RedisConnectionFactory connectionFactory) throws Exception { this.connectionFactory = connectionFactory; @@ -73,7 +75,7 @@ void setUp() { template = new StringRedisTemplate(connectionFactory); template.afterPropertiesSet(); - RedisMappingContext mappingContext = new RedisMappingContext( + mappingContext = new RedisMappingContext( new MappingConfiguration(new IndexConfiguration(), new KeyspaceConfiguration())); mappingContext.afterPropertiesSet(); @@ -225,6 +227,34 @@ void getShouldReadNestedObjectCorrectly() { assertThat(((Person) loaded).address.country).isEqualTo("Andor"); } + @Test // #1995 + void getAllOfShouldReturnSuperTypeIfForUnregisteredTypeAlias() { + + Map map = new LinkedHashMap<>(); + map.put("_class", "taveren"); + map.put("address.country", "Andor"); + template.opsForHash().putAll("persons:load-1", map); + + Object loaded = adapter.get("load-1", "persons", Person.class); + + assertThat(loaded).isExactlyInstanceOf(Person.class); + } + + @Test // #1995 + void getAllOfShouldReturnCorrectTypeIfForRegisteredTypeAlias() { + + mappingContext.getPersistentEntity(TaVeren.class); + + Map map = new LinkedHashMap<>(); + map.put("_class", "taveren"); + map.put("address.country", "Andor"); + template.opsForHash().putAll("persons:load-1", map); + + Object loaded = adapter.get("load-1", "persons", Person.class); + + assertThat(loaded).isExactlyInstanceOf(TaVeren.class); + } + @Test // DATAREDIS-425 void couldReadsKeyspaceSizeCorrectly() { @@ -826,6 +856,7 @@ static class AddressWithPostcode extends Address { String postcode; } + @TypeAlias("taveren") static class TaVeren extends Person { }