-
Notifications
You must be signed in to change notification settings - Fork 682
@DomainEvents method not being called on saveAll with Window<> #2938
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
Comments
Could you provide a minimal reproducer project? |
@quaff here is the demo project https://github.com/jiaheng/spring-data-jpa-domain-event, and these are the steps to reproduce:
curl -X 'POST' \
'http://localhost:8080/my-classes' \
-H 'accept: */*' \
-d ''
curl -X 'POST' \
'http://localhost:8080/my-classes/created/update' \
-H 'accept: */*' \
-d '' Based on the demo code |
It's not minimal, and doesn't contains any test case. |
@quaff I updated spring-data-jpa-domain-event repo to strip away some unnecessary code and component. Also I added 2 test cases to compare the behaviour of using |
It can be minified to this: package com.example.demo.service;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.EventListener;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.springframework.data.domain.KeysetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Repository;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.then;
@EnableJpaRepositories(considerNestedRepositories = true)
@SpringBootTest
class DomainEventHandlerTest {
@Autowired
MyClassRepository myClassRepository;
@Autowired
MyClassService myClassService;
@SpyBean
EventHandler eventHandler;
@Test
void test() {
myClassRepository.save(new MyClass());
myClassService.update();
then(eventHandler).should().handleMyClassUpdateEvent(any());
}
@TestConfiguration
static class Config {
@Bean
MyClassService myClassService(MyClassRepository myClassRepository) {
return new MyClassService(myClassRepository);
}
@Bean
EventHandler eventHandler() {
return new EventHandler();
}
}
@Entity
static class MyClass extends AbstractAggregateRoot<MyClass> {
@Id
@GeneratedValue
private Integer id;
private String field;
public void setField(String field) {
this.field = field;
registerEvent(new MyClassUpdateEvent());
}
}
@Repository
interface MyClassRepository extends JpaRepository<MyClass, Integer> {
Window<MyClass> findFirst1By(KeysetScrollPosition position);
}
static class MyClassUpdateEvent {
}
static class MyClassService {
private final MyClassRepository myClassRepository;
public MyClassService(MyClassRepository myClassRepository) {
this.myClassRepository = myClassRepository;
}
public void update() {
Window<MyClass> myClasses = myClassRepository.findFirst1By(ScrollPosition.keyset());
for (MyClass myClass : myClasses) {
myClass.setField("updated");
}
myClassRepository.saveAll(myClasses); // change myClasses to myClasses.toList() will pass
}
}
static class EventHandler {
@EventListener
public void handleMyClassUpdateEvent(MyClassUpdateEvent event) {
}
}
} |
I believe it's a bug of spring-data-commons, I'm going to fix it. |
repository.saveAll(Window<?>) will be handled properly after this commit. Fixes https://github.com/spring-projects/spring-data-jpa/issues/3153
Got it. Thanks for the suggestion. |
repository.saveAll(Window<?>) will be handled properly after this commit. Fixes https://github.com/spring-projects/spring-data-jpa/issues/3153
repository.saveAll(Window<?>) will be handled properly after this commit. Fixes https://github.com/spring-projects/spring-data-jpa/issues/3153
This has been fixed for the 2.7, 3.0, 3.1 bugfix releases and the 3.2 RC1 coming. I've turned this into a bug as we already had test cases in place that described the feature as being expected to be available but ultimately didn't actually test it was. This is now in place by handling calls to |
I have an entity which will registerEvent when I update the field, and
MyClassModifyEventHandler.handle()
is expected to be executed uponrepository.save()
orrepository.saveAll()
I have a method which will query a list of
MyClass
and update their respective fields.So with this, the
@EventListener
is not working even though the event is registered beforesaveAll()
. If I callingsaveAll
onList<MyClass>
instead (iemyClassRepo.saveAll(myClasses.toList())
), theMyClassModifyEventHandler.handle
will be called. Is this expected behaviour?The text was updated successfully, but these errors were encountered: