DATACMNS-1325 - Added dedicated identifier accessor for Persistable entities.

BasicPersistentEntity now returns a dedicated IdentifierAccessor that uses Persistable.getId() in case the entity implements Persistable.
This commit is contained in:
Oliver Gierke
2018-05-18 23:59:21 +02:00
parent c31e2cb251
commit 1b742306ad
3 changed files with 71 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.AssociationHandler;
import org.springframework.data.mapping.IdentifierAccessor;
@@ -435,6 +436,10 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
Assert.notNull(bean, "Target bean must not be null!");
assertBeanType(bean);
if (Persistable.class.isAssignableFrom(getType())) {
return new PersistableIdentifierAccessor((Persistable<?>) bean);
}
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : NullReturningIdentifierAccessor.INSTANCE;
}

View File

@@ -0,0 +1,32 @@
package org.springframework.data.mapping.model;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.IdentifierAccessor;
/**
* {@link IdentifierAccessor} that invokes {@link Persistable#getId()}.
*
* @author Oliver Gierke
*/
class PersistableIdentifierAccessor implements IdentifierAccessor {
private final Persistable<?> target;
/**
* Creates a new {@link PersistableIdentifierAccessor} for the given target.
*
* @param target must not be {@literal null}.
*/
public PersistableIdentifierAccessor(Persistable<?> target) {
this.target = target;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.IdentifierAccessor#getIdentifier()
*/
@Override
public Object getIdentifier() {
return target.getId();
}
}

View File

@@ -40,6 +40,7 @@ import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentEntitySpec;
@@ -260,6 +261,14 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
});
}
@Test // DATACMNS-1325
public void supportsPersistableViaIdentifierAccessor() {
PersistentEntity<PersistableEntity, T> entity = createEntity(PersistableEntity.class);
assertThat(entity.getIdentifierAccessor(new PersistableEntity()).getIdentifier(), is((Object) 4711L));
}
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
return createEntity(type, null);
}
@@ -299,4 +308,29 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
static class AliasEntityUsingComposedAnnotation {}
static class Subtype extends Entity {}
// DATACMNS-1325
static class PersistableEntity implements Persistable<Long> {
private final Long id = 42L;
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Persistable#getId()
*/
@Override
public Long getId() {
return 4711L;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Persistable#isNew()
*/
@Override
public boolean isNew() {
return false;
}
}
}