Additional changes for SGF-260 supporting @Id annotations on domain entity class or interface methods in addition to domain entity class fields. This change replaces the use of the org.springframework.data.repository.core.support.ReflectionEntityInformation in the DefaultGemfireEntityInformation class with an internal implementation of SDC EntityInformation interface based on the GemfirePersistentEntity instance passed to the DefaultGemfireEntityInformation class during construction.

This commit is contained in:
John Blum
2014-03-26 17:02:59 -07:00
parent c027e73131
commit 7f5eb6edb6
2 changed files with 215 additions and 5 deletions

View File

@@ -18,16 +18,23 @@ 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.data.repository.core.support.ReflectionEntityInformation;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* Implementation of {@link GemfireEntityInformation} using reflection to lookup region names.
*
* Implementation of {@link GemfireEntityInformation} using reflection to lookup entity properties and type information.
* <p/>
* @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
*/
public class DefaultGemfireEntityInformation<T, ID extends Serializable> extends DelegatingEntityInformation<T, ID>
implements GemfireEntityInformation<T, ID> {
implements GemfireEntityInformation<T, ID> {
private final GemfirePersistentEntity<T> entity;
@@ -37,7 +44,8 @@ implements GemfireEntityInformation<T, ID> {
* @param entity must not be {@literal null}.
*/
public DefaultGemfireEntityInformation(GemfirePersistentEntity<T> entity) {
super(new ReflectionEntityInformation<T, ID>(entity.getType()));
//super(new ReflectionEntityInformation<T, ID>(entity.getType()));
super(new PersistentEntityBasedEntityInformation<T, ID>(entity));
this.entity = entity;
}
@@ -49,4 +57,45 @@ implements GemfireEntityInformation<T, ID> {
public String getRegionName() {
return entity.getRegionName();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.AbstractEntityInformation
*/
protected static class PersistentEntityBasedEntityInformation<T, ID extends Serializable>
extends AbstractEntityInformation<T, ID> {
private final GemfirePersistentEntity persistentEntity;
public PersistentEntityBasedEntityInformation(final GemfirePersistentEntity<T> 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<ID> getIdType() {
return (Class<ID>) getEntityIdProperty().getRawType();
}
}
}