I have a state stored aggregate which uses generic jpa repository. I do not want to apply cqrs on this aggregate so that I want to query directly this table to get all aggregates or filter by fields. This is my aggregate repository configuration;
@Bean("customRewardRepository")
fun rewardRepository(): Repository<Reward> =
GenericJpaRepository.builder(Reward::class.java)
.entityManagerProvider(entityManagerProvider)
.eventBus(eventBus)
.build()
How can I query this table by using spring data jpa?
I am not entirely sure what you’re looking for. Mainly because the “using spring data JPA” part is a little vague to me. Would you be able to provide some pseudo-code on what you actually want to do?
Know that on any note, you can always use the EntityManager to query the Aggregate.
Added, I spotted you’ve also constructed a Stack Overflow issue on the matter. I’ve added a comment there stating I’ll proceed here instead of there with help.
Hi Steven, I meant querying with “findAll” or “FindByField” etc. I solved this problem by writing extra jpa repository as @vab2048 suggested. Thank you.
There shouldn’t be an issue with this, like @vab2048 points out! Axon’s GenericJpaRepository uses the EntityManager instead of Spring Data’s regular approach. Henceforth, it’s not the same type of repository as Spring Data constructs.
The naming clash mentioned by @vab2048 is the main thing to look out for. As you’re in a Spring environment, its worth mentioning that Axon’s @Aggregate annotation allows specification of the repository bean to use. This would be done as follows:
// somewhere in a configuration file:
@Bean
public Repository<MyAggregate> myAggregateAxonRepository(EventBus eventBus,
EntityManagerProvider entityManagerProvider) {
return GenericJpaRepository.builder(MyAggregate.class)
.eventBus(eventBus)
.entityManagerProvider(entityManagerProvider)
.build();
}
// somewhere in your aggregate package:
@Aggregate(repository = "
class MyAggregate {
// state and command handlers...
}