Skip to content

Trigger BeforeConvertEvent when saving an aggregate. #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0-gh-910-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0-gh-910-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0-gh-910-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0-gh-910-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ private <T> T triggerAfterLoad(T entity) {
}

private <T> T triggerBeforeConvert(T aggregateRoot) {

publisher.publishEvent(new BeforeConvertEvent<>(aggregateRoot));

return entityCallbacks.callback(BeforeConvertCallback.class, aggregateRoot);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.springframework.data.relational.core.mapping.event.AfterDeleteEvent;
import org.springframework.data.relational.core.mapping.event.AfterLoadEvent;
import org.springframework.data.relational.core.mapping.event.AfterSaveEvent;
import org.springframework.data.relational.core.mapping.event.BeforeConvertEvent;
import org.springframework.data.relational.core.mapping.event.BeforeDeleteEvent;
import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent;
import org.springframework.data.relational.core.mapping.event.Identifier;
Expand Down Expand Up @@ -115,6 +116,7 @@ public void publishesEventsOnSave() {
assertThat(publisher.events) //
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
BeforeConvertEvent.class, //
BeforeSaveEvent.class, //
AfterSaveEvent.class //
);
Expand All @@ -132,8 +134,10 @@ public void publishesEventsOnSaveMany() {
assertThat(publisher.events) //
.extracting(e -> (Class) e.getClass()) //
.containsExactly( //
BeforeConvertEvent.class, //
BeforeSaveEvent.class, //
AfterSaveEvent.class, //
BeforeConvertEvent.class, //
BeforeSaveEvent.class, //
AfterSaveEvent.class //
);
Expand Down
4 changes: 2 additions & 2 deletions spring-data-relational/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-relational</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0-gh-910-SNAPSHOT</version>

<name>Spring Data Relational</name>
<description>Spring Data Relational support</description>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.2.0-SNAPSHOT</version>
<version>2.2.0-gh-910-SNAPSHOT</version>
</parent>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @since 1.1
* @author Jens Schauder
*/
public class BeforeConvertEvent<E> extends RelationalSaveEvent<E> {
public class BeforeConvertEvent<E> extends RelationalEventWithEntity<E> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the type hierarchy, especially when targeting backports is asking for trouble. The initial design of this class was already problematic as we never had an AggregateChange in the first place.


private static final long serialVersionUID = -5716795164911939224L;

Expand All @@ -33,7 +33,7 @@ public class BeforeConvertEvent<E> extends RelationalSaveEvent<E> {
* this event is fired before the conversion the change is actually empty, but contains information if the
* aggregate is considered new in {@link AggregateChange#getKind()}. Must not be {@literal null}.
*/
public BeforeConvertEvent(E instance, AggregateChange<E> change) {
super(instance, change);
public BeforeConvertEvent(E instance) {
super(instance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void afterLoad() {
@Test // DATAJDBC-454
public void beforeConvert() {

listener.onApplicationEvent(new BeforeConvertEvent<>(dummyEntity, MutableAggregateChange.forDelete(dummyEntity)));
listener.onApplicationEvent(new BeforeConvertEvent<>(dummyEntity));

assertThat(events).containsExactly("beforeConvert");
}
Expand Down