Skip to content

Commit 1b74230

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 c31e2cb commit 1b74230

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-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
import org.slf4j.LoggerFactory;
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.Association;
3839
import org.springframework.data.mapping.AssociationHandler;
3940
import org.springframework.data.mapping.IdentifierAccessor;
@@ -435,6 +436,10 @@ public IdentifierAccessor getIdentifierAccessor(Object bean) {
435436
Assert.notNull(bean, "Target bean must not be null!");
436437
assertBeanType(bean);
437438

439+
if (Persistable.class.isAssignableFrom(getType())) {
440+
return new PersistableIdentifierAccessor((Persistable<?>) bean);
441+
}
442+
438443
return hasIdProperty() ? new IdPropertyIdentifierAccessor(this, bean) : NullReturningIdentifierAccessor.INSTANCE;
439444
}
440445

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.springframework.data.mapping.model;
2+
3+
import org.springframework.data.domain.Persistable;
4+
import org.springframework.data.mapping.IdentifierAccessor;
5+
6+
/**
7+
* {@link IdentifierAccessor} that invokes {@link Persistable#getId()}.
8+
*
9+
* @author Oliver Gierke
10+
*/
11+
class PersistableIdentifierAccessor implements IdentifierAccessor {
12+
13+
private final Persistable<?> target;
14+
15+
/**
16+
* Creates a new {@link PersistableIdentifierAccessor} for the given target.
17+
*
18+
* @param target must not be {@literal null}.
19+
*/
20+
public PersistableIdentifierAccessor(Persistable<?> target) {
21+
this.target = target;
22+
}
23+
24+
/*
25+
* (non-Javadoc)
26+
* @see org.springframework.data.mapping.IdentifierAccessor#getIdentifier()
27+
*/
28+
@Override
29+
public Object getIdentifier() {
30+
return target.getId();
31+
}
32+
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.annotation.CreatedDate;
4141
import org.springframework.data.annotation.LastModifiedBy;
4242
import org.springframework.data.annotation.TypeAlias;
43+
import org.springframework.data.domain.Persistable;
4344
import org.springframework.data.mapping.Association;
4445
import org.springframework.data.mapping.PersistentEntity;
4546
import org.springframework.data.mapping.PersistentEntitySpec;
@@ -260,6 +261,14 @@ public void doWithAssociation(Association<? extends PersistentProperty<?>> assoc
260261
});
261262
}
262263

264+
@Test // DATACMNS-1325
265+
public void supportsPersistableViaIdentifierAccessor() {
266+
267+
PersistentEntity<PersistableEntity, T> entity = createEntity(PersistableEntity.class);
268+
269+
assertThat(entity.getIdentifierAccessor(new PersistableEntity()).getIdentifier(), is((Object) 4711L));
270+
}
271+
263272
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
264273
return createEntity(type, null);
265274
}
@@ -299,4 +308,29 @@ public String getProperty() {
299308
static class AliasEntityUsingComposedAnnotation {}
300309

301310
static class Subtype extends Entity {}
311+
312+
// DATACMNS-1325
313+
314+
static class PersistableEntity implements Persistable<Long> {
315+
316+
private final Long id = 42L;
317+
318+
/*
319+
* (non-Javadoc)
320+
* @see org.springframework.data.domain.Persistable#getId()
321+
*/
322+
@Override
323+
public Long getId() {
324+
return 4711L;
325+
}
326+
327+
/*
328+
* (non-Javadoc)
329+
* @see org.springframework.data.domain.Persistable#isNew()
330+
*/
331+
@Override
332+
public boolean isNew() {
333+
return false;
334+
}
335+
}
302336
}

0 commit comments

Comments
 (0)