From 2157bb404df06a6b79c84eb3b8f18121a62ca3b7 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 16 Jan 2017 09:34:47 +0100 Subject: [PATCH 1/2] DATACMNS-976 - AbstractEntityInformation should consider Persistable.isNew(). Prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e3fdc53620..e8d9e5fa80 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-commons - 1.13.0.BUILD-SNAPSHOT + 1.13.0.DATACMNS-976-SNAPSHOT Spring Data Core From 9ab87af036b2e1bd4c13c7ec3767204216ed1c0c Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Mon, 16 Jan 2017 09:37:16 +0100 Subject: [PATCH 2/2] DATACMNS-976 - AbstractEntityInformation considers Persistable.isNew(). We now consider Persistable.isNew() implementations when checking isNew(entity) on an persistent entity. --- .../support/AbstractEntityInformation.java | 8 ++++- .../AbstractEntityInformationUnitTests.java | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/repository/core/support/AbstractEntityInformation.java b/src/main/java/org/springframework/data/repository/core/support/AbstractEntityInformation.java index e31d13d0a4..bc1956e6d9 100644 --- a/src/main/java/org/springframework/data/repository/core/support/AbstractEntityInformation.java +++ b/src/main/java/org/springframework/data/repository/core/support/AbstractEntityInformation.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2017 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. @@ -17,6 +17,7 @@ import java.io.Serializable; +import org.springframework.data.domain.Persistable; import org.springframework.data.repository.core.EntityInformation; import org.springframework.util.Assert; @@ -26,6 +27,7 @@ * * @author Oliver Gierke * @author Nick Williams + * @author Christoph Strobl */ public abstract class AbstractEntityInformation implements EntityInformation { @@ -48,6 +50,10 @@ public AbstractEntityInformation(Class domainClass) { */ public boolean isNew(T entity) { + if(entity instanceof Persistable) { + return ((Persistable) entity).isNew(); + } + ID id = getId(entity); Class idType = getIdType(); diff --git a/src/test/java/org/springframework/data/repository/core/support/AbstractEntityInformationUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/AbstractEntityInformationUnitTests.java index 09a0cff78b..b6ac23c134 100644 --- a/src/test/java/org/springframework/data/repository/core/support/AbstractEntityInformationUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/AbstractEntityInformationUnitTests.java @@ -18,12 +18,15 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import lombok.Builder; + import java.io.Serializable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.data.annotation.Id; +import org.springframework.data.domain.Persistable; import org.springframework.data.repository.core.EntityInformation; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.util.ReflectionUtils; @@ -33,6 +36,7 @@ * * @author Oliver Gierke * @author Nick Williams + * @author Christoph Strobl */ public class AbstractEntityInformationUnitTests { @@ -88,6 +92,17 @@ public void rejectsUnsupportedPrimitiveIdType() { information.isNew(new UnsupportedPrimitiveIdEntity()); } + @Test // DATACMNS-976 + public void considersPersistableIsNew() { + + FooEn information = new FooEn(ImplementingPersistable.class); + + assertThat(information.isNew(ImplementingPersistable.builder().id(100L).isNew(true).build()), is(true)); + assertThat(information.isNew(ImplementingPersistable.builder().isNew(true).build()), is(true)); + assertThat(information.isNew(ImplementingPersistable.builder().id(100L).isNew(false).build()), is(false)); + assertThat(information.isNew(ImplementingPersistable.builder().isNew(false).build()), is(false)); + } + static class PrimitiveIdEntity { @Id long id; @@ -124,4 +139,21 @@ public Class getIdType() { return (Class) ReflectionUtils.findField(type, "id").getType(); } } + + @Builder + static class ImplementingPersistable implements Persistable { + + final Long id; + final boolean isNew; + + @Override + public Long getId() { + return id; + } + + @Override + public boolean isNew() { + return isNew; + } + } }