Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
* @author Mikhail Polivakha
* @author Jens Schauder
* @author Christoph Strobl
* @author leewoo97
* @since 1.1
*/
public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAware, ApplicationContextAware {
Expand Down Expand Up @@ -742,12 +743,19 @@ private <T> Mono<T> doUpsert(T entity, SqlIdentifier tableName, OutboundRow outb
}

List<SqlIdentifier> updateColumns = new ArrayList<>();
Set<SqlIdentifier> insertOnlyColumns = new LinkedHashSet<>();
persistentEntity.forEach(p -> {
if (!p.isInsertOnly() && !identifierColumns.contains(p.getColumnName())) {
updateColumns.add(p.getColumnName());
if (p.isInsertOnly()) {
insertOnlyColumns.add(p.getColumnName());
}
});

for (SqlIdentifier column : outboundRow.keySet()) {
if (!identifierColumns.contains(column) && !insertOnlyColumns.contains(column)) {
updateColumns.add(column);
}
}

upsert = upsert.withUpdateColumns(updateColumns);

PreparedOperation<?> operation = mapper.getMappedObject(upsert);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
import org.springframework.data.r2dbc.mapping.R2dbcMappingContext;
import org.springframework.data.r2dbc.testing.StatementRecorder;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Embedded;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.r2dbc.core.Parameter;

/**
* Unit tests for {@link ReactiveUpsertOperation}.
*
* @author Christoph Strobl
* @author leewoo97
*/
public class ReactiveUpsertOperationUnitTests {

Expand Down Expand Up @@ -158,6 +161,25 @@ void upsertIncludesInsertOnlyColumns() {
assertThat(statement.getSql()).contains("insert_only");
}

@Test // GH-2313
void upsertDoesNotUpdateEmbeddedIdProperty() {

MockRowMetadata metadata = MockRowMetadata.builder().build();
MockResult result = MockResult.builder().rowMetadata(metadata).rowsUpdated(1).build();

recorder.addStubbing(s -> s.startsWith("INSERT"), result);

entityTemplate.upsert(new EntityWithEmbeddedId(new EntityId("one", "two"), "Walter")) //
.as(StepVerifier::create) //
.expectNextCount(1) //
.verifyComplete();

StatementRecorder.RecordedStatement statement = recorder.getCreatedStatement(s -> s.startsWith("INSERT"));

assertThat(statement.getSql()).isEqualTo(
"INSERT INTO entity (col1, col2, name) VALUES ($1, $2, $3) ON CONFLICT (col1, col2) DO UPDATE SET name = EXCLUDED.name");
}

static class Person {

@Id Long id;
Expand All @@ -184,4 +206,11 @@ public void setName(String name) {
}
}

@Table("entity")
record EntityWithEmbeddedId(@Id @Embedded.Empty EntityId id, String name) {
}

record EntityId(String col1, String col2) {
}

}
Loading