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 4563b9467..4029f8de3 100644 --- a/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java +++ b/src/main/java/org/springframework/data/redis/core/RedisKeyValueAdapter.java @@ -605,9 +605,9 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter } }); - if (timeout != null || (timeout == null && !ttlProperty.getType().isPrimitive())) { + if (timeout != null || !ttlProperty.getType().isPrimitive()) { entity.getPropertyAccessor(target).setProperty(ttlProperty, - NumberUtils.convertNumberToTargetClass(timeout, (Class) ttlProperty.getType())); + converter.getConversionService().convert(timeout, ttlProperty.getType())); } } 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 f4c333fd2..886a7ba17 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 @@ -282,9 +282,9 @@ public class RedisMappingContext extends KeyValueMappingContext { } else if (ttlProperty != null) { RedisPersistentEntity entity = mappingContext.getPersistentEntity(type); - Long timeout = (Long) entity.getPropertyAccessor(source).getProperty(ttlProperty); + Number timeout = (Number) entity.getPropertyAccessor(source).getProperty(ttlProperty); if (timeout != null) { - return TimeUnit.SECONDS.convert(timeout, unit); + return TimeUnit.SECONDS.convert(timeout.longValue(), unit); } } else { diff --git a/src/main/java/org/springframework/data/redis/core/mapping/RedisPersistentEntity.java b/src/main/java/org/springframework/data/redis/core/mapping/RedisPersistentEntity.java index eaac9807b..1997c0df2 100644 --- a/src/main/java/org/springframework/data/redis/core/mapping/RedisPersistentEntity.java +++ b/src/main/java/org/springframework/data/redis/core/mapping/RedisPersistentEntity.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -43,7 +43,7 @@ public interface RedisPersistentEntity extends KeyValuePersistentEntity { boolean hasExplictTimeToLiveProperty(); /** - * Get the {@link PersistentProperty} + * Get the {@link PersistentProperty} that is annotated with {@link org.springframework.data.redis.core.TimeToLive}. * * @return can be {@null}. * @since 1.8 diff --git a/src/test/java/org/springframework/data/redis/core/RedisKeyValueTemplateTests.java b/src/test/java/org/springframework/data/redis/core/RedisKeyValueTemplateTests.java index 7d79b2bf6..897de3fac 100644 --- a/src/test/java/org/springframework/data/redis/core/RedisKeyValueTemplateTests.java +++ b/src/test/java/org/springframework/data/redis/core/RedisKeyValueTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 the original author or authors. + * Copyright 2015-2016 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. @@ -48,7 +48,10 @@ import lombok.Data; import lombok.EqualsAndHashCode; /** + * Integration tests for {@link RedisKeyValueTemplate}. + * * @author Christoph Strobl + * @author Mark Paluch */ @RunWith(Parameterized.class) public class RedisKeyValueTemplateTests { @@ -887,6 +890,25 @@ public class RedisKeyValueTemplateTests { assertThat(target.ttl.doubleValue(), is(closeTo(3D, 1D))); } + /** + * @see DATAREDIS-523 + */ + @Test + public void shouldReadBackExplicitTimeToLiveToPrimitiveField() throws InterruptedException { + + WithPrimitiveTtl source = new WithPrimitiveTtl(); + source.id = "ttl-1"; + source.ttl = 5; + source.value = "5 seconds"; + + template.insert(source); + + Thread.sleep(1100); + + WithPrimitiveTtl target = template.findById(source.id, WithPrimitiveTtl.class); + assertThat((double) target.ttl, is(closeTo(3D, 1D))); + } + /** * @see DATAREDIS-523 */ @@ -1049,4 +1071,12 @@ public class RedisKeyValueTemplateTests { String value; @TimeToLive Long ttl; } + + @Data + static class WithPrimitiveTtl { + + @Id String id; + String value; + @TimeToLive int ttl; + } }