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

Entity callbacks not fired for String-based queries or named query methods #591

Closed
JoseLion opened this issue Apr 29, 2021 · 4 comments
Closed
Labels
type: bug A general bug

Comments

@JoseLion
Copy link
Contributor

Hi,

I'm trying to use entity callbacks like AfterConvertCallback or BeforeConvertCallback in my project, which work great for built-in query methods like .findAll() or findById(ID id). However, it seems they are not being fired for String-based queries or named query methods.

I might be missing something, but I tracked the issue a little bit, and it seems the entity callbacks are only called from the R2dbcEntityTemplate methods. As far as I could tell, string-based queries and named query methods don't go through these methods (like select(Query query, Class entityClass) or insert(T entity)).

I'd be more than happy to help if someone could point me in the right direction. It seems that this could need some design discussion to probably move the callbacks to a higher level, or maybe just call them from the places where other queries run too. In any case, I'm always happy to contribute if possible. 🙂

Versions

  • Spring Boot: v2.4.5
  • JVM: Java(TM) SE Runtime Environment (build 15.0.2+7-27)

Code sample

@Configuration
public class EmployeeCallbacks {

  @Bean
  public AfterConvertCallback<Employee> afterConvert() {
    return (employee, table) -> {
      // Do something with the entity
    };
  }
}
public interface EmployeeRepository extends ReactiveCrudRepository<Employee, Long> {

  @Query("SELECT * FROM employee WHERE name=:name")
  Mono<Employee> findFirstByName(String name); // This never triggers the callback ❌

  Mono<Employee> findFirstByLastname(String lastname); // This never triggers the callback ❌

  // Mono<Employee> findById(Long id); // This is built-in, this always triggers the callback ✅
}

As always, thanks for the great work on this project.

Cheers!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 29, 2021
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 29, 2021
@mp911de
Copy link
Member

mp911de commented May 4, 2021

The difference in behavior comes from the differences in running the SQL statement. SimpleR2dbcRepository uses R2dbcEntityOperations which makes use of entity callbacks while query methods directly use DatabaseClient. We need to migrate query method execution into R2dbcEntityOperations so we can reuse entity callbacks.

@mp911de mp911de added this to the 1.4 M1 (2021.1.0) milestone May 4, 2021
@mp911de mp911de closed this as completed in 6732561 May 4, 2021
@JoseLion
Copy link
Contributor Author

JoseLion commented May 6, 2021

Awesome, thank you @mp911de! Entity callbacks are extremely helpful. I use them to eagerly populate relational fields (one to one, one to many, etc.), which need to be transient in the model for the time being.

Just for me to be on the lookout, it's possible to know in which version of Spring Boot will this be available?

Thank again!!

@mp911de
Copy link
Member

mp911de commented May 6, 2021

The next Boot generation that will pick up these changes is going to be Spring Boot 2.6. There's new API and quite a bit of rewrite required to make this work and that bears a certain risk.

@JoseLion
Copy link
Contributor Author

JoseLion commented May 6, 2021

Got it, totally understandable too! I'll be on the watch for v2.6 to give it a try. Thanks again @mp911de 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants