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.
This commit is contained in:
@@ -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}.
|
||||
* <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
|
||||
* @see org.springframework.data.repository.core.support.PersistentEntityInformation
|
||||
*/
|
||||
public class DefaultGemfireEntityInformation<T, ID extends Serializable> extends DelegatingEntityInformation<T, ID>
|
||||
public class DefaultGemfireEntityInformation<T, ID extends Serializable> extends PersistentEntityInformation<T, ID>
|
||||
implements GemfireEntityInformation<T, ID> {
|
||||
|
||||
private final GemfirePersistentEntity<T> entity;
|
||||
|
||||
/**
|
||||
* Creates a new {@link DefaultGemfireEntityInformation}.
|
||||
*
|
||||
*
|
||||
* @param entity must not be {@literal null}.
|
||||
*/
|
||||
public DefaultGemfireEntityInformation(GemfirePersistentEntity<T> entity) {
|
||||
//super(new ReflectionEntityInformation<T, ID>(entity.getType()));
|
||||
super(new PersistentEntityBasedEntityInformation<T, ID>(entity));
|
||||
super(entity);
|
||||
this.entity = entity;
|
||||
}
|
||||
|
||||
@@ -58,44 +56,4 @@ public class DefaultGemfireEntityInformation<T, ID extends Serializable> extends
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -112,7 +112,6 @@ public class DefaultGemfireEntityInformationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
// Uh, WOW!
|
||||
public void testConfusedDomainEntityHavingStringId() {
|
||||
GemfireEntityInformation<MyConfusedDomainEntity, String> 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")
|
||||
|
||||
Reference in New Issue
Block a user