Skip to content

Commit 87c2ac5

Browse files
JoseLionmp911de
authored andcommitted
Use BeforeConvert callback result on non-versioned updates.
Previously, we used the original entity. Closes #689 Original pull request: #690.
1 parent f3b85bd commit 87c2ac5

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

src/main/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplate.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
* @author Mark Paluch
9090
* @author Bogdan Ilchyshyn
9191
* @author Jens Schauder
92+
* @author Jose Luis Leon
9293
* @since 1.1
9394
*/
9495
public class R2dbcEntityTemplate implements R2dbcEntityOperations, BeanFactoryAware, ApplicationContextAware {
@@ -686,7 +687,7 @@ private <T> Mono<T> doUpdate(T entity, SqlIdentifier tableName) {
686687
entityToUse = incrementVersion(persistentEntity, it);
687688
} else {
688689

689-
entityToUse = entity;
690+
entityToUse = it;
690691
matchingVersionCriteria = null;
691692
}
692693

src/test/java/org/springframework/data/r2dbc/core/R2dbcEntityTemplateUnitTests.java

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
* Unit tests for {@link R2dbcEntityTemplate}.
6868
*
6969
* @author Mark Paluch
70+
* @author Jose Luis Leon
7071
*/
7172
public class R2dbcEntityTemplateUnitTests {
7273

@@ -245,8 +246,8 @@ void shouldDeleteByQuery() {
245246
@Test // gh-220
246247
void shouldDeleteEntity() {
247248

248-
Person person = new Person();
249-
person.id = "Walter";
249+
Person person = Person.empty() //
250+
.withId("Walter");
250251
recorder.addStubbing(s -> s.startsWith("DELETE"), Collections.emptyList());
251252

252253
entityTemplate.delete(person) //
@@ -390,7 +391,7 @@ void insertShouldInvokeCallback() {
390391
ValueCapturingAfterSaveCallback afterSave = new ValueCapturingAfterSaveCallback();
391392

392393
entityTemplate.setEntityCallbacks(ReactiveEntityCallbacks.create(beforeConvert, beforeSave, afterSave));
393-
entityTemplate.insert(new Person()).as(StepVerifier::create) //
394+
entityTemplate.insert(Person.empty()).as(StepVerifier::create) //
394395
.assertNext(actual -> {
395396
assertThat(actual.id).isEqualTo("after-save");
396397
assertThat(actual.name).isEqualTo("before-convert");
@@ -439,10 +440,10 @@ void updateShouldInvokeCallback() {
439440
ValueCapturingBeforeSaveCallback beforeSave = new ValueCapturingBeforeSaveCallback();
440441
ValueCapturingAfterSaveCallback afterSave = new ValueCapturingAfterSaveCallback();
441442

442-
Person person = new Person();
443-
person.id = "the-id";
444-
person.name = "name";
445-
person.description = "description";
443+
Person person = Person.empty() //
444+
.withId("the-id") //
445+
.withName("name") //
446+
.withDescription("description");
446447

447448
entityTemplate.setEntityCallbacks(ReactiveEntityCallbacks.create(beforeConvert, beforeSave, afterSave));
448449
entityTemplate.update(person).as(StepVerifier::create) //
@@ -460,7 +461,8 @@ void updateShouldInvokeCallback() {
460461
Parameter.from("before-save"));
461462
}
462463

463-
@ToString
464+
@Value
465+
@With
464466
static class Person {
465467

466468
@Id String id;
@@ -469,12 +471,8 @@ static class Person {
469471

470472
String description;
471473

472-
public String getName() {
473-
return name;
474-
}
475-
476-
public void setName(String name) {
477-
this.name = name;
474+
public static Person empty() {
475+
return new Person(null, null, null);
478476
}
479477
}
480478

@@ -548,8 +546,8 @@ static class ValueCapturingBeforeConvertCallback extends ValueCapturingEntityCal
548546
public Mono<Person> onBeforeConvert(Person entity, SqlIdentifier table) {
549547

550548
capture(entity);
551-
entity.name = "before-convert";
552-
return Mono.just(entity);
549+
Person person = entity.withName("before-convert");
550+
return Mono.just(person);
553551
}
554552
}
555553

@@ -573,9 +571,9 @@ public Mono<Person> onAfterSave(Person entity, OutboundRow outboundRow, SqlIdent
573571

574572
capture(entity);
575573

576-
Person person = new Person();
577-
person.id = "after-save";
578-
person.name = entity.name;
574+
Person person = Person.empty() //
575+
.withId("after-save") //
576+
.withName(entity.getName());
579577

580578
return Mono.just(person);
581579
}
@@ -588,9 +586,9 @@ static class ValueCapturingAfterConvertCallback extends ValueCapturingEntityCall
588586
public Mono<Person> onAfterConvert(Person entity, SqlIdentifier table) {
589587

590588
capture(entity);
591-
Person person = new Person();
592-
person.id = "after-convert";
593-
person.name = entity.name;
589+
Person person = Person.empty() //
590+
.withId("after-convert") //
591+
.withName(entity.getName());
594592

595593
return Mono.just(person);
596594
}

0 commit comments

Comments
 (0)