Skip to content

Commit 7bd7afe

Browse files
committed
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.
1 parent 0d31ab6 commit 7bd7afe

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.springframework.core.annotation.AnnotatedElementUtils;
2727
import org.springframework.data.annotation.TypeAlias;
28+
import org.springframework.data.domain.Persistable;
2829
import org.springframework.data.mapping.*;
2930
import org.springframework.data.util.Lazy;
3031
import org.springframework.data.util.TypeInformation;
@@ -441,6 +442,10 @@ public IdentifierAccessor getIdentifierAccessor(Object bean) {
441442
Assert.isTrue(getType().isInstance(bean),
442443
() -> String.format(TYPE_MISMATCH, bean.getClass().getName(), getType().getName()));
443444

445+
if (Persistable.class.isAssignableFrom(getType())) {
446+
return new PersistableIdentifierAccessor((Persistable<?>) bean);
447+
}
448+
444449
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : new AbsentIdentifierAccessor(bean);
445450
}
446451

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mapping.model;
17+
18+
import org.springframework.data.domain.Persistable;
19+
import org.springframework.data.mapping.IdentifierAccessor;
20+
import org.springframework.data.mapping.TargetAwareIdentifierAccessor;
21+
import org.springframework.lang.Nullable;
22+
23+
/**
24+
* {@link IdentifierAccessor} that invokes {@link Persistable#getId()}.
25+
*
26+
* @author Oliver Gierke
27+
*/
28+
class PersistableIdentifierAccessor extends TargetAwareIdentifierAccessor {
29+
30+
private final Persistable<?> target;
31+
32+
/**
33+
* Creates a new {@link PersistableIdentifierAccessor} for the given target.
34+
*
35+
* @param target must not be {@literal null}.
36+
*/
37+
public PersistableIdentifierAccessor(Persistable<?> target) {
38+
39+
super(target);
40+
41+
this.target = target;
42+
}
43+
44+
/*
45+
* (non-Javadoc)
46+
* @see org.springframework.data.mapping.IdentifierAccessor#getIdentifier()
47+
*/
48+
@Override
49+
@Nullable
50+
public Object getIdentifier() {
51+
return target.getId();
52+
}
53+
}

src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.data.annotation.CreatedDate;
3939
import org.springframework.data.annotation.LastModifiedBy;
4040
import org.springframework.data.annotation.TypeAlias;
41+
import org.springframework.data.domain.Persistable;
4142
import org.springframework.data.mapping.Alias;
4243
import org.springframework.data.mapping.Document;
4344
import org.springframework.data.mapping.MappingException;
@@ -276,6 +277,14 @@ public void getRequiredAnnotationThrowsException() {
276277
assertThatThrownBy(() -> entity.getRequiredAnnotation(Document.class)).isInstanceOf(IllegalStateException.class);
277278
}
278279

280+
@Test // DATACMNS-1325
281+
public void supportsPersistableViaIdentifierAccessor() {
282+
283+
PersistentEntity<PersistableEntity, T> entity = createEntity(PersistableEntity.class);
284+
285+
assertThat(entity.getIdentifierAccessor(new PersistableEntity()).getRequiredIdentifier()).isEqualTo(4711L);
286+
}
287+
279288
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
280289
return createEntity(type, null);
281290
}
@@ -315,4 +324,29 @@ public String getProperty() {
315324
static class AliasEntityUsingComposedAnnotation {}
316325

317326
static class Subtype extends Entity {}
327+
328+
// DATACMNS-1325
329+
330+
static class PersistableEntity implements Persistable<Long> {
331+
332+
private final Long id = 42L;
333+
334+
/*
335+
* (non-Javadoc)
336+
* @see org.springframework.data.domain.Persistable#getId()
337+
*/
338+
@Override
339+
public Long getId() {
340+
return 4711L;
341+
}
342+
343+
/*
344+
* (non-Javadoc)
345+
* @see org.springframework.data.domain.Persistable#isNew()
346+
*/
347+
@Override
348+
public boolean isNew() {
349+
return false;
350+
}
351+
}
318352
}

0 commit comments

Comments
 (0)