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 ac881f4e..74bec9c8 100644 --- a/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntity.java +++ b/src/main/java/org/springframework/data/gemfire/mapping/GemfirePersistentEntity.java @@ -19,6 +19,8 @@ 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; @@ -28,9 +30,12 @@ import org.springframework.data.gemfire.mapping.annotation.LocalRegion; import org.springframework.data.gemfire.mapping.annotation.PartitionRegion; import org.springframework.data.gemfire.mapping.annotation.Region; import org.springframework.data.gemfire.mapping.annotation.ReplicateRegion; +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 @@ -38,6 +43,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 */ @@ -48,12 +54,61 @@ public class GemfirePersistentEntity extends BasicPersistentEntity { + + try { + Method getMethod = bean.getClass().getMethod(GET_ID_METHOD_NAME); + + if (getMethod == null) + return null; // getId method not found + + return getMethod.invoke(bean); + } catch (NoSuchMethodException e) { + return null; + } catch (SecurityException e) { + return null; + } catch (IllegalAccessException e) { + return null; + } catch (IllegalArgumentException e) { + return null; + } catch (InvocationTargetException e) { + return null; + } + + }; + + return getIdAccessor; + } + /* (non-Javadoc) */ protected static Annotation resolveRegionAnnotation(Class 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; @@ -66,8 +121,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()); } @@ -87,12 +143,12 @@ public class GemfirePersistentEntity extends BasicPersistentEntity concrete {@link Class} type of the Region {@link Annotation}. - * @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. + * @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 ClientRegion * @see LocalRegion * @see PartitionRegion @@ -106,11 +162,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( @@ -65,8 +99,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"))); @@ -79,8 +113,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"))); @@ -90,15 +124,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")