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 d73e0a1f8f
commit 865163d466
3 changed files with 92 additions and 0 deletions

View File

@@ -34,6 +34,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.Alias;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.AssociationHandler;
@@ -473,6 +474,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();
}
}