Skip to content
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

Introduce standardized interfaces for published application events #3057

Open
BriceRoncace opened this issue Feb 28, 2024 · 3 comments
Open
Labels
type: enhancement A general enhancement

Comments

@BriceRoncace
Copy link

A possible enhancement to functionality detailed in the docs at https://docs.spring.io/spring-data/data-jpa/reference/data-commons/repositories/core-domain-events.html.

Any time, say, the save method is called (from CrudRepository), standard Spring created events (like Spring Data REST's BeforeSaveEvent and AfterSaveEvent) would be published. This would allow developers to create custom event handlers for standard lifecycle events without having to register custom events on those entities.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 28, 2024
@mp911de
Copy link
Member

mp911de commented Feb 28, 2024

The events you refer to are events provided by Spring Data REST (org.springframework.data.rest.core.event). In addition, each store module comes with their own events such as JPA's @PrePersist. Store-specific lifecycle events follow store semantics so for instance JPA events might be delayed until the transaction is closed.

Other stores like MongoDB or Cassandra emit events directly when updating or saving an object.

That being said, it doesn't make sense to introduce another mechanism over the existing ones. Let us know whether that helps.

@mp911de mp911de added the status: waiting-for-feedback We need additional information before we can continue label Feb 28, 2024
@BriceRoncace
Copy link
Author

Thank you @mp911de. In our use case we are exploring various ways to perform additional work right before an entity is saved (persisted or updated in JPA event terms). Our application is a traditional web application built with Spring Boot 3 (spring-boot-starter-web and spring-boot-starter-data-jpa); we are not using a specialized store, just a JPA compatible relational database.

We've tried:

  • JPA lifecycle events - but because a child object may be updated and not the parent, the aggregate root's @PreUpdate, for example, won't fire as desired.
  • Custom JPA repositories having enhanced save methods (think cleanAndSave)
  • Overriding Spring Data repository save methods
  • AOP
  • Domain events published by the repository

This last point prompted this request. We fully agree with Oliver Drotbohm's statement:

Currently Spring Data JPA expects calls to ….save(…) as it's the only way we can reliably flag save point and issue the event distribution. It's the developer signaling, a set of state changes is really supposed to be persistent.

And organize our code in this way (i.e. explicitly calling repositories rather than relying on implicit changes to managed entities). But this limitation to JPA's lifecycle events prompted us to keep exploring options.

So, while I understand what you mean by introducing another event mechanism alongside the existing one (i.e., standard JPA events), I think having general-purpose, Spring domain events published by a Spring Data repository to signal that an entity was created, saved or deleted (like Spring Data REST) could be a nice way to standardize how Spring Data repositories behave within the Spring ecosystem (i.e., publishing application events). And, in our use case anyway, would eliminate some of the annoyances we have with JPA's lifecycle events not firing when we expect them to.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 28, 2024
@mp911de mp911de added the for: team-attention An issue we need to discuss as a team to make progress label Feb 29, 2024
@mp911de mp911de transferred this issue from spring-projects/spring-data-jpa Mar 4, 2024
@mp911de mp911de changed the title Feature Request: Publish standard lifecycle domain events Introduce standardized interfaces for published application events Mar 4, 2024
@mp911de
Copy link
Member

mp911de commented Mar 4, 2024

Thanks for sharing your thoughts. There's indeed a impedance mismatch between the general repository abstraction and the lifecycle event handling. While repository interfaces are shared, events arose from store-specific implementations without a consolidation.

JPA clearly complicates the picture because JPA allows saving all sorts of things that aren't necessarily considered domain types by Spring Data.

We might come up with an interface-based approach to let our events implement these interfaces for a better abstraction.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided for: team-attention An issue we need to discuss as a team to make progress labels Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants