Skip to content

Commit 865163d

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 d73e0a1 commit 865163d

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
@@ -34,6 +34,7 @@
3434

3535
import org.springframework.core.annotation.AnnotatedElementUtils;
3636
import org.springframework.data.annotation.TypeAlias;
37+
import org.springframework.data.domain.Persistable;
3738
import org.springframework.data.mapping.Alias;
3839
import org.springframework.data.mapping.Association;
3940
import org.springframework.data.mapping.AssociationHandler;
@@ -473,6 +474,10 @@ public IdentifierAccessor getIdentifierAccessor(Object bean) {
473474
Assert.isTrue(getType().isInstance(bean),
474475
() -> String.format(TYPE_MISMATCH, bean.getClass().getName(), getType().getName()));
475476

477+
if (Persistable.class.isAssignableFrom(getType())) {
478+
return new PersistableIdentifierAccessor((Persistable<?>) bean);
479+
}
480+
476481
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : new AbsentIdentifierAccessor(bean);
477482
}
478483

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
@@ -44,6 +44,7 @@
4444
import org.springframework.data.annotation.LastModifiedBy;
4545
import org.springframework.data.annotation.Persistent;
4646
import org.springframework.data.annotation.TypeAlias;
47+
import org.springframework.data.domain.Persistable;
4748
import org.springframework.data.mapping.Alias;
4849
import org.springframework.data.mapping.Document;
4950
import org.springframework.data.mapping.MappingException;
@@ -338,6 +339,14 @@ public <A extends Annotation> A findAnnotation(Class<A> annotationType) {
338339
assertThat(failed.get()).isFalse();
339340
}
340341

342+
@Test // DATACMNS-1325
343+
public void supportsPersistableViaIdentifierAccessor() {
344+
345+
PersistentEntity<PersistableEntity, T> entity = createEntity(PersistableEntity.class);
346+
347+
assertThat(entity.getIdentifierAccessor(new PersistableEntity()).getRequiredIdentifier()).isEqualTo(4711L);
348+
}
349+
341350
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
342351
return createEntity(type, null);
343352
}
@@ -382,4 +391,29 @@ static class Subtype extends Entity {}
382391
static class EntityWithAnnotation {
383392

384393
}
394+
395+
// DATACMNS-1325
396+
397+
static class PersistableEntity implements Persistable<Long> {
398+
399+
private final Long id = 42L;
400+
401+
/*
402+
* (non-Javadoc)
403+
* @see org.springframework.data.domain.Persistable#getId()
404+
*/
405+
@Override
406+
public Long getId() {
407+
return 4711L;
408+
}
409+
410+
/*
411+
* (non-Javadoc)
412+
* @see org.springframework.data.domain.Persistable#isNew()
413+
*/
414+
@Override
415+
public boolean isNew() {
416+
return false;
417+
}
418+
}
385419
}

0 commit comments

Comments
 (0)