From d400c78fb65b4a3b4dfc352ff76dae37f4ec0ce3 Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 27 Mar 2014 12:44:25 -0700 Subject: [PATCH] Completes JIRA ticket SGF-260 adding support for @Id annotated domain entity interface and class methods in addition to domain entity class fields. This change replaces the DefaultGemfireEntityInformation class's PersistentEntityBasedEntityInformation implementation of the EntityInformation interface to use the new Spring Data Commons org.springframework.data.repository.core.support.PersistentEntityInformation class. Now, DefaultGemfireEntityInformation extends PersistentEntityInformation directly and uses the GemfirePersistentEntity mapping meta-data to retrieve information about the domain entity's id. --- .../DefaultGemfireEntityInformation.java | 62 +++---------------- .../DefaultGemfireEntityInformationTest.java | 5 +- 2 files changed, 12 insertions(+), 55 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformation.java b/src/main/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformation.java index c04eb292..ff7dde84 100644 --- a/src/main/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformation.java +++ b/src/main/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformation.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2014 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. @@ -13,39 +13,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.data.gemfire.repository.query; import java.io.Serializable; import org.springframework.data.gemfire.mapping.GemfirePersistentEntity; -import org.springframework.data.mapping.PersistentProperty; -import org.springframework.data.repository.core.support.AbstractEntityInformation; -import org.springframework.data.repository.core.support.DelegatingEntityInformation; -import org.springframework.util.Assert; -import org.springframework.util.ReflectionUtils; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.repository.core.support.PersistentEntityInformation; /** - * Implementation of {@link GemfireEntityInformation} using reflection to lookup entity properties and type information. + * Implementation of {@link GemfireEntityInformation} to return the region name stored in the backing + * {@link PersistentEntity}. *

* @author Oliver Gierke * @author John Blum * @see org.springframework.data.gemfire.mapping.GemfirePersistentEntity * @see org.springframework.data.gemfire.repository.query.GemfireEntityInformation - * @see org.springframework.data.repository.core.support.DelegatingEntityInformation + * @see org.springframework.data.repository.core.support.PersistentEntityInformation */ -public class DefaultGemfireEntityInformation extends DelegatingEntityInformation +public class DefaultGemfireEntityInformation extends PersistentEntityInformation implements GemfireEntityInformation { private final GemfirePersistentEntity entity; /** * Creates a new {@link DefaultGemfireEntityInformation}. - * + * * @param entity must not be {@literal null}. */ public DefaultGemfireEntityInformation(GemfirePersistentEntity entity) { - //super(new ReflectionEntityInformation(entity.getType())); - super(new PersistentEntityBasedEntityInformation(entity)); + super(entity); this.entity = entity; } @@ -58,44 +56,4 @@ public class DefaultGemfireEntityInformation extends return entity.getRegionName(); } - /* - * (non-Javadoc) - * @see org.springframework.data.repository.core.support.AbstractEntityInformation - */ - protected static class PersistentEntityBasedEntityInformation - extends AbstractEntityInformation { - - private final GemfirePersistentEntity persistentEntity; - - public PersistentEntityBasedEntityInformation(final GemfirePersistentEntity persistentEntity) { - super(persistentEntity.getType()); - this.persistentEntity = persistentEntity; - } - - protected GemfirePersistentEntity getPersistentEntity() { - return persistentEntity; - } - - protected PersistentProperty getEntityIdProperty() { - Assert.state(getPersistentEntity().hasIdProperty(), String.format( - "The ID property of entity type (%1$s) was not properly discovered!", - getPersistentEntity().getType())); - - return getPersistentEntity().getIdProperty(); - } - - @SuppressWarnings("unchecked") - public ID getId(final T entity) { - PersistentProperty idProperty = getEntityIdProperty(); - - return (ID) (idProperty.usePropertyAccess() ? ReflectionUtils.invokeMethod(idProperty.getGetter(), entity) - : ReflectionUtils.getField(idProperty.getField(), entity)); - } - - @SuppressWarnings("unchecked") - public Class getIdType() { - return (Class) getEntityIdProperty().getRawType(); - } - } - } diff --git a/src/test/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformationTest.java b/src/test/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformationTest.java index a4d8ae42..b4d8f5c7 100644 --- a/src/test/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformationTest.java +++ b/src/test/java/org/springframework/data/gemfire/repository/query/DefaultGemfireEntityInformationTest.java @@ -112,7 +112,6 @@ public class DefaultGemfireEntityInformationTest { } @Test - // Uh, WOW! public void testConfusedDomainEntityHavingStringId() { GemfireEntityInformation entityInfo = createEntityInformation( createPersistentEntity(MyConfusedDomainEntity.class)); @@ -122,8 +121,8 @@ public class DefaultGemfireEntityInformationTest { assertEquals(MyConfusedDomainEntity.class, entityInfo.getJavaType()); //assertEquals(String.class, entityInfo.getIdType()); assertTrue(Long.class.equals(entityInfo.getIdType())); - //assertEquals("123", entityInfo.getId(new MyConfusedDomainEntity(123l))); - assertEquals(123l, entityInfo.getId(new MyConfusedDomainEntity("123"))); + assertEquals(123l, entityInfo.getId(new MyConfusedDomainEntity(123l))); + assertEquals(248l, entityInfo.getId(new MyConfusedDomainEntity("248"))); } @SuppressWarnings("unused")