From 3fc74622160da38ba8d53d3ba8c15162cdba14cc Mon Sep 17 00:00:00 2001 From: currenjin Date: Wed, 30 Jul 2025 19:35:19 +0900 Subject: [PATCH] refactor: extract magic number 0L to constant for new entity check Signed-off-by: currenjin --- .../support/AbstractEntityInformation.java | 3 ++- .../AbstractEntityInformationUnitTests.java | 22 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) 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 c9081b4fe6..4d6286549f 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 @@ -30,6 +30,7 @@ */ public abstract class AbstractEntityInformation implements EntityInformation { + public static final long NEW_ID = 0L; private final Class domainClass; public AbstractEntityInformation(Class domainClass) { @@ -50,7 +51,7 @@ public boolean isNew(T entity) { } if (id instanceof Number n) { - return n.longValue() == 0L; + return n.longValue() == NEW_ID; } throw new IllegalArgumentException(String.format("Unsupported primitive id type %s", idType)); 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 a7bb9c868d..f55195f498 100755 --- a/src/test/java/org/springframework/data/repository/core/support/AbstractEntityInformationUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/AbstractEntityInformationUnitTests.java @@ -49,7 +49,7 @@ void considersEntityNewIfGetIdReturnsNull() throws Exception { } @Test // DATACMNS-357 - void detectsNewStateForPrimitiveIds() { + void detectsNewStateForZeroIds() { var fooEn = new CustomEntityInformation( PrimitiveIdEntity.class); @@ -61,6 +61,16 @@ void detectsNewStateForPrimitiveIds() { assertThat(fooEn.isNew(entity)).isFalse(); } + @Test // DATACMNS-357 + void detectsNewStateForPrimitiveIds() { + + var fooEn = new CustomEntityInformation( + PrimitiveWithIdEntity.class); + + var entity = new PrimitiveWithIdEntity(0L); + assertThat(fooEn.isNew(entity)).isTrue(); + } + @Test // DATACMNS-357 void detectsNewStateForPrimitiveWrapperIds() { @@ -85,6 +95,16 @@ void rejectsUnsupportedPrimitiveIdType() { .withMessageContaining(boolean.class.getName()); } + static class PrimitiveWithIdEntity { + + @Id long id; + + + public PrimitiveWithIdEntity(long id) { + this.id = id; + } + } + static class PrimitiveIdEntity { @Id long id;