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:41:39 +02:00
parent 0d31ab693d
commit 7bd7afe6fb
3 changed files with 92 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ import java.util.stream.Collectors;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.*;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeInformation;
@@ -441,6 +442,10 @@ public class BasicPersistentEntity<T, P extends PersistentProperty<P>> implement
Assert.isTrue(getType().isInstance(bean),
() -> String.format(TYPE_MISMATCH, bean.getClass().getName(), getType().getName()));
if (Persistable.class.isAssignableFrom(getType())) {
return new PersistableIdentifierAccessor((Persistable<?>) bean);
}
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : new AbsentIdentifierAccessor(bean);
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mapping.model;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.IdentifierAccessor;
import org.springframework.data.mapping.TargetAwareIdentifierAccessor;
import org.springframework.lang.Nullable;
/**
* {@link IdentifierAccessor} that invokes {@link Persistable#getId()}.
*
* @author Oliver Gierke
*/
class PersistableIdentifierAccessor extends TargetAwareIdentifierAccessor {
private final Persistable<?> target;
/**
* Creates a new {@link PersistableIdentifierAccessor} for the given target.
*
* @param target must not be {@literal null}.
*/
public PersistableIdentifierAccessor(Persistable<?> target) {
super(target);
this.target = target;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.IdentifierAccessor#getIdentifier()
*/
@Override
@Nullable
public Object getIdentifier() {
return target.getId();
}
}

View File

@@ -38,6 +38,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.Alias;
import org.springframework.data.mapping.Document;
import org.springframework.data.mapping.MappingException;
@@ -276,6 +277,14 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
assertThatThrownBy(() -> entity.getRequiredAnnotation(Document.class)).isInstanceOf(IllegalStateException.class);
}
@Test // DATACMNS-1325
public void supportsPersistableViaIdentifierAccessor() {
PersistentEntity<PersistableEntity, T> entity = createEntity(PersistableEntity.class);
assertThat(entity.getIdentifierAccessor(new PersistableEntity()).getRequiredIdentifier()).isEqualTo(4711L);
}
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
return createEntity(type, null);
}
@@ -315,4 +324,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;
}
}
}