From 41b335000fe30ba1e3c442d131923e64093d8788 Mon Sep 17 00:00:00 2001 From: Gregory Green Date: Thu, 5 Jan 2017 08:50:17 -0500 Subject: [PATCH] SGF-582 - Add support for using bean property "id" as a Region key if @Id annotation is not present. (cherry picked from commit 3bc7d2e7115aac93727b04004f809f5f345a1e2d) Signed-off-by: John Blum --- .../mapping/GemfirePersistentEntity.java | 92 +++++++++++++++++-- .../GemfirePersistentEntityUnitTests.java | 57 ++++++++++-- 2 files changed, 129 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntity.java b/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntity.java index 911a47c8..1f0f7f67 100644 --- a/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntity.java +++ b/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntity.java @@ -19,13 +19,18 @@ package org.springframework.data.gemfire.mapping; import static org.springframework.data.gemfire.util.SpringUtils.defaultIfEmpty; import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.data.mapping.IdentifierAccessor; import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.mapping.model.BasicPersistentEntity; +import org.springframework.data.mapping.model.IdPropertyIdentifierAccessor; import org.springframework.data.util.TypeInformation; +import org.springframework.util.Assert; /** * {@link PersistentEntity} implementation adding custom GemFire persistent entity related metadata, such as the @@ -33,6 +38,7 @@ import org.springframework.data.util.TypeInformation; * * @author Oliver Gierke * @author John Blum + * @author Gregory Green * @see org.springframework.data.gemfire.mapping.GemfirePersistentProperty * @see org.springframework.data.mapping.model.BasicPersistentEntity */ @@ -43,12 +49,67 @@ public class GemfirePersistentEntity extends BasicPersistentEntity persistentEntityType) { for (Class regionAnnotationType : Region.REGION_ANNOTATION_TYPES) { - Annotation regionAnnotation = AnnotatedElementUtils.getMergedAnnotation( - persistentEntityType, regionAnnotationType); + Annotation regionAnnotation = AnnotatedElementUtils.getMergedAnnotation(persistentEntityType, + regionAnnotationType); if (regionAnnotation != null) { return regionAnnotation; @@ -61,8 +122,9 @@ public class GemfirePersistentEntity extends BasicPersistentEntity persistentEntityType, Annotation regionAnnotation) { - String regionName = (regionAnnotation != null ? AnnotationAttributes.fromMap( - AnnotationUtils.getAnnotationAttributes(regionAnnotation)).getString("value") : null); + String regionName = (regionAnnotation != null + ? AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(regionAnnotation)).getString("value") + : null); return defaultIfEmpty(regionName, persistentEntityType.getSimpleName()); } @@ -82,10 +144,11 @@ public class GemfirePersistentEntity extends BasicPersistentEntity concrete {@link Class} type of the Region {@link Annotation}. +<<<<<<< HEAD * @return the {@link Region} annotation used to annotate this {@link PersistentEntity} or {@literal null} * if this {@link PersistentEntity} was not annotated with a {@link Region} annotation. * @see org.springframework.data.gemfire.mapping.ClientRegion @@ -93,6 +156,15 @@ public class GemfirePersistentEntity extends BasicPersistentEntity>>>>>> 3bc7d2e... SGF-582 - Add support for using bean property "id" as a Region key if @Id annotation is not present. * @see java.lang.annotation.Annotation */ @SuppressWarnings("unchecked") @@ -101,11 +173,11 @@ public class GemfirePersistentEntity extends BasicPersistentEntity unnamedRegionEntity = new GemfirePersistentEntity( + ClassTypeInformation.from(UnannotatedRegion.class)); + + IdentifierAccessor accessor = unnamedRegionEntity.getIdentifierAccessor(new UnannotatedRegion()); + Assert.assertNull(accessor.getIdentifier()); + + GemfirePersistentEntity entity = new GemfirePersistentEntity( + ClassTypeInformation.from(NotAnnotationIdRegion.class)); + + NotAnnotationIdRegion region = new NotAnnotationIdRegion(); + + region.setId("id"); + region.setValue("value"); + + accessor = entity.getIdentifierAccessor(region); + + Assert.assertNotNull(accessor.getIdentifier()); + + Assert.assertEquals("id", accessor.getIdentifier()); + + } + @Test public void defaultsRegionNameToClassName() { GemfirePersistentEntity entity = new GemfirePersistentEntity( @@ -64,8 +98,8 @@ public class GemfirePersistentEntityUnitTests { @Test @SuppressWarnings("unchecked") public void bigDecimalPersistentPropertyIsNotEntity() { - GemfirePersistentEntity entity = (GemfirePersistentEntity) - getMappingContext().getPersistentEntity(ExampleDomainObject.class); + GemfirePersistentEntity entity = (GemfirePersistentEntity) getMappingContext() + .getPersistentEntity(ExampleDomainObject.class); assertThat(entity.getRegionName(), is(equalTo("Example"))); @@ -78,8 +112,8 @@ public class GemfirePersistentEntityUnitTests { @Test @SuppressWarnings("unchecked") public void bigIntegerPersistentPropertyIsNotEntity() { - GemfirePersistentEntity entity = (GemfirePersistentEntity) - getMappingContext().getPersistentEntity(ExampleDomainObject.class); + GemfirePersistentEntity entity = (GemfirePersistentEntity) getMappingContext() + .getPersistentEntity(ExampleDomainObject.class); assertThat(entity.getRegionName(), is(equalTo("Example"))); @@ -89,15 +123,18 @@ public class GemfirePersistentEntityUnitTests { assertThat(bigNumber.isEntity(), is(false)); } - static class UnannotatedRegion { - } + static class UnannotatedRegion {} @Region("Foo") - static class AnnotatedRegion { - } + static class AnnotatedRegion {} @Region - static class UnnamedRegion { + static class UnnamedRegion {} + + static @Data class NotAnnotationIdRegion { + + private String value; + private String id; } @Region("Example")