|
43 | 43 | import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
|
44 | 44 | import org.springframework.lang.Nullable;
|
45 | 45 |
|
| 46 | +import lombok.Data; |
46 | 47 | import lombok.RequiredArgsConstructor;
|
47 | 48 |
|
48 | 49 | /**
|
@@ -99,6 +100,48 @@ public void newEntityGetsConvertedToOneInsert() {
|
99 | 100 | );
|
100 | 101 | }
|
101 | 102 |
|
| 103 | + @Test |
| 104 | + void newEntityWithPrimitiveLongId_insertDoesNotIncludeId_whenIdValueIsZero() { |
| 105 | + PrimitiveLongIdEntity entity = new PrimitiveLongIdEntity(); |
| 106 | + |
| 107 | + MutableAggregateChange<PrimitiveLongIdEntity> aggregateChange = // |
| 108 | + new DefaultAggregateChange<>(AggregateChange.Kind.SAVE, PrimitiveLongIdEntity.class, entity); |
| 109 | + |
| 110 | + converter.write(entity, aggregateChange); |
| 111 | + |
| 112 | + assertThat(extractActions(aggregateChange)) // |
| 113 | + .extracting(DbAction::getClass, // |
| 114 | + DbAction::getEntityType, // |
| 115 | + DbActionTestSupport::extractPath, // |
| 116 | + DbActionTestSupport::actualEntityType, // |
| 117 | + DbActionTestSupport::isWithDependsOn, // |
| 118 | + DbActionTestSupport::insertIncludeId) // |
| 119 | + .containsExactly( // |
| 120 | + tuple(InsertRoot.class, PrimitiveLongIdEntity.class, "", PrimitiveLongIdEntity.class, false, false) // |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void newEntityWithPrimitiveIntId_insertDoesNotIncludeId_whenIdValueIsZero() { |
| 126 | + PrimitiveIntIdEntity entity = new PrimitiveIntIdEntity(); |
| 127 | + |
| 128 | + MutableAggregateChange<PrimitiveIntIdEntity> aggregateChange = // |
| 129 | + new DefaultAggregateChange<>(AggregateChange.Kind.SAVE, PrimitiveIntIdEntity.class, entity); |
| 130 | + |
| 131 | + converter.write(entity, aggregateChange); |
| 132 | + |
| 133 | + assertThat(extractActions(aggregateChange)) // |
| 134 | + .extracting(DbAction::getClass, // |
| 135 | + DbAction::getEntityType, // |
| 136 | + DbActionTestSupport::extractPath, // |
| 137 | + DbActionTestSupport::actualEntityType, // |
| 138 | + DbActionTestSupport::isWithDependsOn, // |
| 139 | + DbActionTestSupport::insertIncludeId) // |
| 140 | + .containsExactly( // |
| 141 | + tuple(InsertRoot.class, PrimitiveIntIdEntity.class, "", PrimitiveIntIdEntity.class, false, false) // |
| 142 | + ); |
| 143 | + } |
| 144 | + |
102 | 145 | @Test // DATAJDBC-111
|
103 | 146 | public void newEntityGetsConvertedToOneInsertByEmbeddedEntities() {
|
104 | 147 |
|
@@ -146,6 +189,32 @@ public void newEntityWithReferenceGetsConvertedToTwoInserts() {
|
146 | 189 | );
|
147 | 190 | }
|
148 | 191 |
|
| 192 | + @Test |
| 193 | + void newEntityWithReference_whenReferenceHasPrimitiveId_insertDoesNotIncludeId_whenIdValueIsZero() { |
| 194 | + |
| 195 | + EntityWithReferencesToPrimitiveIdEntity entity = new EntityWithReferencesToPrimitiveIdEntity(null); |
| 196 | + entity.primitiveLongIdEntity = new PrimitiveLongIdEntity(); |
| 197 | + entity.primitiveIntIdEntity = new PrimitiveIntIdEntity(); |
| 198 | + |
| 199 | + MutableAggregateChange<EntityWithReferencesToPrimitiveIdEntity> aggregateChange = // |
| 200 | + new DefaultAggregateChange<>(AggregateChange.Kind.SAVE, EntityWithReferencesToPrimitiveIdEntity.class, entity); |
| 201 | + |
| 202 | + converter.write(entity, aggregateChange); |
| 203 | + |
| 204 | + assertThat(extractActions(aggregateChange)) // |
| 205 | + .extracting(DbAction::getClass, // |
| 206 | + DbAction::getEntityType, // |
| 207 | + DbActionTestSupport::extractPath, // |
| 208 | + DbActionTestSupport::actualEntityType, // |
| 209 | + DbActionTestSupport::isWithDependsOn, // |
| 210 | + DbActionTestSupport::insertIncludeId) // |
| 211 | + .containsExactlyInAnyOrder( // |
| 212 | + tuple(InsertRoot.class, EntityWithReferencesToPrimitiveIdEntity.class, "", EntityWithReferencesToPrimitiveIdEntity.class, false, false), // |
| 213 | + tuple(Insert.class, PrimitiveLongIdEntity.class, "primitiveLongIdEntity", PrimitiveLongIdEntity.class, true, false), // |
| 214 | + tuple(Insert.class, PrimitiveIntIdEntity.class, "primitiveIntIdEntity", PrimitiveIntIdEntity.class, true, false) // |
| 215 | + ); |
| 216 | + } |
| 217 | + |
149 | 218 | @Test // DATAJDBC-112
|
150 | 219 | public void existingEntityGetsConvertedToDeletePlusUpdate() {
|
151 | 220 |
|
@@ -726,6 +795,32 @@ void newEntityWithCollectionWhereSomeElementsHaveIdSet_producesABatchInsertEachF
|
726 | 795 | );
|
727 | 796 | }
|
728 | 797 |
|
| 798 | + @Test |
| 799 | + void newEntityWithCollection_whenElementHasPrimitiveId_batchInsertDoesNotIncludeId_whenIdValueIsZero() { |
| 800 | + |
| 801 | + EntityWithReferencesToPrimitiveIdEntity entity = new EntityWithReferencesToPrimitiveIdEntity(null); |
| 802 | + entity.primitiveLongIdEntities.add(new PrimitiveLongIdEntity()); |
| 803 | + entity.primitiveIntIdEntities.add(new PrimitiveIntIdEntity()); |
| 804 | + |
| 805 | + MutableAggregateChange<EntityWithReferencesToPrimitiveIdEntity> aggregateChange = // |
| 806 | + new DefaultAggregateChange<>(AggregateChange.Kind.SAVE, EntityWithReferencesToPrimitiveIdEntity.class, entity); |
| 807 | + |
| 808 | + converter.write(entity, aggregateChange); |
| 809 | + |
| 810 | + List<DbAction<?>> actions = extractActions(aggregateChange); |
| 811 | + assertThat(actions).extracting(DbAction::getClass, // |
| 812 | + DbAction::getEntityType, // |
| 813 | + DbActionTestSupport::extractPath, // |
| 814 | + DbActionTestSupport::actualEntityType, // |
| 815 | + DbActionTestSupport::isWithDependsOn, // |
| 816 | + DbActionTestSupport::insertIncludeId) // |
| 817 | + .containsExactlyInAnyOrder( // |
| 818 | + tuple(InsertRoot.class, EntityWithReferencesToPrimitiveIdEntity.class, "", EntityWithReferencesToPrimitiveIdEntity.class, false, false), // |
| 819 | + tuple(Insert.class, PrimitiveLongIdEntity.class, "primitiveLongIdEntities", PrimitiveLongIdEntity.class, true, false), // |
| 820 | + tuple(Insert.class, PrimitiveIntIdEntity.class, "primitiveIntIdEntities", PrimitiveIntIdEntity.class, true, false) // |
| 821 | + ); |
| 822 | + } |
| 823 | + |
729 | 824 | private List<DbAction<?>> extractActions(MutableAggregateChange<?> aggregateChange) {
|
730 | 825 |
|
731 | 826 | List<DbAction<?>> actions = new ArrayList<>();
|
@@ -798,6 +893,26 @@ static PersistentPropertyPath<RelationalPersistentProperty> toPath(String path,
|
798 | 893 | return persistentPropertyPaths.filter(p -> p.toDotPath().equals(path)).stream().findFirst().orElse(null);
|
799 | 894 | }
|
800 | 895 |
|
| 896 | + @RequiredArgsConstructor |
| 897 | + @Data |
| 898 | + static class EntityWithReferencesToPrimitiveIdEntity { |
| 899 | + @Id final Long id; |
| 900 | + PrimitiveLongIdEntity primitiveLongIdEntity; |
| 901 | + List<PrimitiveLongIdEntity> primitiveLongIdEntities = new ArrayList<>(); |
| 902 | + PrimitiveIntIdEntity primitiveIntIdEntity; |
| 903 | + List<PrimitiveIntIdEntity> primitiveIntIdEntities = new ArrayList<>(); |
| 904 | + } |
| 905 | + |
| 906 | + @Data |
| 907 | + static class PrimitiveLongIdEntity { |
| 908 | + @Id long id; |
| 909 | + } |
| 910 | + |
| 911 | + @Data |
| 912 | + static class PrimitiveIntIdEntity { |
| 913 | + @Id int id; |
| 914 | + } |
| 915 | + |
801 | 916 | @RequiredArgsConstructor
|
802 | 917 | static class SingleReferenceEntity {
|
803 | 918 |
|
|
0 commit comments