How to query state stored aggregate

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;

    fun rewardRepository(): Repository<Reward> =

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.

You can create your Spring Data Repository like normal (assuming the ID type is Long):

public interface RewardRepository extends JpaRepository<Reward, Long> {}

Then autowire it wherever you want to use it.

Note - there may be a naming clash with the Axon repository - so you may need to use the @Qualifier annotation when using @Autowire.

1 Like

Hi Steven, I meant querying with “findAll” or “FindByField” etc. I solved this problem by writing extra jpa repository as @vab2048 suggested. Thank you.

Thank you for your advice, do you think is there any problem by having two repositories for one entity?

The only problem would be a possible name clash for beans. But you can avoid this by using the @Qualifier annotation.

Glad I could help :+1:

1 Like

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:
public Repository<MyAggregate> myAggregateAxonRepository(EventBus eventBus,
                                                         EntityManagerProvider entityManagerProvider) {
    return GenericJpaRepository.builder(MyAggregate.class)

// somewhere in your aggregate package:
@Aggregate(repository = "
class MyAggregate {

    // state and command handlers...