Skip to content

Commit 5ecb550

Browse files
committed
DATACMNS-1326 - PersistentPropertyInspectingIsNewStrategy now considers primitive type's defaults.
PersistentPropertyInspectingIsNewStrategy now considers entities with primitive default identifier values new.
1 parent 7bd7afe commit 5ecb550

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/main/java/org/springframework/data/mapping/context/MappingContextIsNewStrategyFactory.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected IsNewStrategy doGetIsNewStrategy(Class<?> type) {
8585
if (entity.hasIdProperty()) {
8686

8787
return PersistentPropertyInspectingIsNewStrategy.of(entity.getRequiredIdProperty(),
88-
MappingContextIsNewStrategyFactory::propertyIsNull);
88+
MappingContextIsNewStrategyFactory::propertyIsNullOrZeroNumber);
8989
}
9090

9191
return null;
@@ -116,11 +116,7 @@ public boolean isNew(Object entity) {
116116
}
117117
}
118118

119-
private static boolean propertyIsNull(Object it) {
120-
return it == null;
121-
}
122-
123119
private static boolean propertyIsNullOrZeroNumber(Object value) {
124-
return propertyIsNull(value) || value instanceof Number && ((Number) value).longValue() == 0;
120+
return value == null || value instanceof Number && ((Number) value).longValue() == 0;
125121
}
126122
}

src/test/java/org/springframework/data/mapping/context/MappingContextIsNewStrategyFactoryUnitTests.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
4242
public void setUp() {
4343

4444
SampleMappingContext context = new SampleMappingContext();
45-
context.setInitialEntitySet(new HashSet<>(Arrays.asList(Entity.class, VersionedEntity.class)));
45+
context.setInitialEntitySet(
46+
new HashSet<>(Arrays.asList(Entity.class, VersionedEntity.class, PrimitiveIdEntity.class)));
4647
context.afterPropertiesSet();
4748

4849
factory = new MappingContextIsNewStrategyFactory(new PersistentEntities(Collections.singleton(context)));
@@ -93,6 +94,18 @@ public void returnsPropertyIsNullIsNewStrategyForEntity() {
9394
assertThat(strategy.isNew(entity)).isFalse();
9495
}
9596

97+
@Test // DATACMNS-1326
98+
public void entityWithPrimitiveDefaultIsNotConsideredNew() {
99+
100+
IsNewStrategy strategy = factory.getIsNewStrategy(PrimitiveIdEntity.class);
101+
102+
PrimitiveIdEntity entity = new PrimitiveIdEntity();
103+
assertThat(strategy.isNew(entity)).isTrue();
104+
105+
entity.id = 1L;
106+
assertThat(strategy.isNew(entity)).isFalse();
107+
}
108+
96109
@SuppressWarnings("serial")
97110
static class PersistableEntity implements Persistable<Long> {
98111

@@ -118,7 +131,7 @@ static class VersionedEntity {
118131
@Id Long id;
119132
}
120133

121-
static class PrimitveVersionedEntity {
134+
static class PrimitiveVersionedEntity {
122135

123136
@Version long version = 0;
124137

@@ -129,4 +142,8 @@ static class Entity {
129142

130143
@Id Long id;
131144
}
145+
146+
static class PrimitiveIdEntity {
147+
@Id long id;
148+
}
132149
}

0 commit comments

Comments
 (0)