DATACMNS-1326 - PersistentPropertyInspectingIsNewStrategy now considers primitive type's defaults.
PersistentPropertyInspectingIsNewStrategy now considers entities with primitive default identifier values new.
This commit is contained in:
@@ -85,7 +85,7 @@ public class MappingContextIsNewStrategyFactory extends IsNewStrategyFactorySupp
|
||||
if (entity.hasIdProperty()) {
|
||||
|
||||
return PersistentPropertyInspectingIsNewStrategy.of(entity.getRequiredIdProperty(),
|
||||
MappingContextIsNewStrategyFactory::propertyIsNull);
|
||||
MappingContextIsNewStrategyFactory::propertyIsNullOrZeroNumber);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -116,11 +116,7 @@ public class MappingContextIsNewStrategyFactory extends IsNewStrategyFactorySupp
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean propertyIsNull(Object it) {
|
||||
return it == null;
|
||||
}
|
||||
|
||||
private static boolean propertyIsNullOrZeroNumber(Object value) {
|
||||
return propertyIsNull(value) || value instanceof Number && ((Number) value).longValue() == 0;
|
||||
return value == null || value instanceof Number && ((Number) value).longValue() == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
|
||||
public void setUp() {
|
||||
|
||||
SampleMappingContext context = new SampleMappingContext();
|
||||
context.setInitialEntitySet(new HashSet<>(Arrays.asList(Entity.class, VersionedEntity.class)));
|
||||
context.setInitialEntitySet(
|
||||
new HashSet<>(Arrays.asList(Entity.class, VersionedEntity.class, PrimitiveIdEntity.class)));
|
||||
context.afterPropertiesSet();
|
||||
|
||||
factory = new MappingContextIsNewStrategyFactory(new PersistentEntities(Collections.singleton(context)));
|
||||
@@ -93,6 +94,18 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
|
||||
assertThat(strategy.isNew(entity)).isFalse();
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1326
|
||||
public void entityWithPrimitiveDefaultIsNotConsideredNew() {
|
||||
|
||||
IsNewStrategy strategy = factory.getIsNewStrategy(PrimitiveIdEntity.class);
|
||||
|
||||
PrimitiveIdEntity entity = new PrimitiveIdEntity();
|
||||
assertThat(strategy.isNew(entity)).isTrue();
|
||||
|
||||
entity.id = 1L;
|
||||
assertThat(strategy.isNew(entity)).isFalse();
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
static class PersistableEntity implements Persistable<Long> {
|
||||
|
||||
@@ -118,7 +131,7 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
|
||||
@Id Long id;
|
||||
}
|
||||
|
||||
static class PrimitveVersionedEntity {
|
||||
static class PrimitiveVersionedEntity {
|
||||
|
||||
@Version long version = 0;
|
||||
|
||||
@@ -129,4 +142,8 @@ public class MappingContextIsNewStrategyFactoryUnitTests {
|
||||
|
||||
@Id Long id;
|
||||
}
|
||||
|
||||
static class PrimitiveIdEntity {
|
||||
@Id long id;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user