#402 - Exclude id property using initial value when inserting objects.

We now exclude Id properties from being used in the INSERT field list if the Id value is zero and of a primitive type or if the value is null using a numeric wrapper type.
This commit is contained in:
Mark Paluch
2020-08-05 14:19:50 +02:00
parent ecbb8d8e78
commit 67c3d492a2
3 changed files with 69 additions and 12 deletions

View File

@@ -20,6 +20,7 @@ import static org.mockito.Mockito.*;
import io.r2dbc.spi.Row;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import java.time.Instant;
import java.time.LocalDateTime;
@@ -189,6 +190,24 @@ public class MappingR2dbcConverterUnitTests {
assertThat(result.entity).isNotNull();
}
@Test // gh-402
public void writeShouldSkipPrimitiveIdIfValueIsZero() {
OutboundRow row = new OutboundRow();
converter.write(new WithPrimitiveId(0), row);
assertThat(row).isEmpty();
}
@Test // gh-402
public void writeShouldWritePrimitiveIdIfValueIsNonZero() {
OutboundRow row = new OutboundRow();
converter.write(new WithPrimitiveId(1), row);
assertThat(row).containsEntry(SqlIdentifier.unquoted("id"), Parameter.fromOrEmpty(1L, Long.TYPE));
}
@AllArgsConstructor
static class Person {
@Id String id;
@@ -214,6 +233,12 @@ public class MappingR2dbcConverterUnitTests {
NonMappableEntity unsupported;
}
@RequiredArgsConstructor
static class WithPrimitiveId {
@Id final long id;
}
static class CustomConversionPerson {
String foo;