|
33 | 33 | import org.junit.jupiter.api.extension.ExtendWith;
|
34 | 34 | import org.mockito.junit.jupiter.MockitoExtension;
|
35 | 35 | import org.springframework.data.annotation.Id;
|
| 36 | +import org.springframework.data.annotation.ReadOnlyProperty; |
36 | 37 | import org.springframework.data.mapping.PersistentPropertyPath;
|
37 | 38 | import org.springframework.data.mapping.PersistentPropertyPaths;
|
38 | 39 | import org.springframework.data.relational.core.conversion.DbAction.Delete;
|
@@ -817,6 +818,46 @@ void newEntityWithCollection_whenElementHasPrimitiveId_batchInsertDoesNotInclude
|
817 | 818 | );
|
818 | 819 | }
|
819 | 820 |
|
| 821 | + @Test // GH-1249 |
| 822 | + public void readOnlyReferenceDoesNotCreateInsertsOnCreation() { |
| 823 | + |
| 824 | + WithReadOnlyReference entity = new WithReadOnlyReference(null); |
| 825 | + entity.readOnly = new Element(SOME_ENTITY_ID); |
| 826 | + |
| 827 | + AggregateChangeWithRoot<WithReadOnlyReference> aggregateChange = MutableAggregateChange.forSave(entity); |
| 828 | + |
| 829 | + new RelationalEntityWriter<WithReadOnlyReference>(context).write(entity, aggregateChange); |
| 830 | + |
| 831 | + assertThat(extractActions(aggregateChange)) // |
| 832 | + .extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath, |
| 833 | + DbActionTestSupport::actualEntityType, DbActionTestSupport::isWithDependsOn) // |
| 834 | + .containsExactly( // |
| 835 | + tuple(InsertRoot.class, WithReadOnlyReference.class, "", WithReadOnlyReference.class, false) // |
| 836 | + // no insert for element |
| 837 | + ); |
| 838 | + |
| 839 | + } |
| 840 | + |
| 841 | + @Test // GH-1249 |
| 842 | + public void readOnlyReferenceDoesNotCreateDeletesOrInsertsDuringUpdate() { |
| 843 | + |
| 844 | + WithReadOnlyReference entity = new WithReadOnlyReference(SOME_ENTITY_ID); |
| 845 | + entity.readOnly = new Element(SOME_ENTITY_ID); |
| 846 | + |
| 847 | + AggregateChangeWithRoot<WithReadOnlyReference> aggregateChange = MutableAggregateChange.forSave(entity); |
| 848 | + |
| 849 | + new RelationalEntityWriter<WithReadOnlyReference>(context).write(entity, aggregateChange); |
| 850 | + |
| 851 | + assertThat(extractActions(aggregateChange)) // |
| 852 | + .extracting(DbAction::getClass, DbAction::getEntityType, DbActionTestSupport::extractPath, |
| 853 | + DbActionTestSupport::actualEntityType, DbActionTestSupport::isWithDependsOn) // |
| 854 | + .containsExactly( // |
| 855 | + tuple(UpdateRoot.class, WithReadOnlyReference.class, "", WithReadOnlyReference.class, false) // |
| 856 | + // no insert for element |
| 857 | + ); |
| 858 | + |
| 859 | + } |
| 860 | + |
820 | 861 | private List<DbAction<?>> extractActions(MutableAggregateChange<?> aggregateChange) {
|
821 | 862 |
|
822 | 863 | List<DbAction<?>> actions = new ArrayList<>();
|
@@ -1015,4 +1056,13 @@ private static class NoIdElement {
|
1015 | 1056 | // empty classes feel weird.
|
1016 | 1057 | String name;
|
1017 | 1058 | }
|
| 1059 | + |
| 1060 | + @RequiredArgsConstructor |
| 1061 | + private static class WithReadOnlyReference { |
| 1062 | + |
| 1063 | + @Id final Long id; |
| 1064 | + @ReadOnlyProperty |
| 1065 | + Element readOnly; |
| 1066 | + } |
| 1067 | + |
1018 | 1068 | }
|
0 commit comments