Question about Repositories

Good Morning/Afternoon/Night

Today I was trying to save data in my query side. However, when I try to add code for using another jpa repository the application stop saving data in the domain_entry table. I tried to use the command handler and add the code for saving data and stop saving again. Remove it and works. Then I try to put the same code in the event handler and again stop saving data in the domain_entry table. I remove it and it is saving to the domain_entry table again. Could you please guide me where is the right place to add this code? Here is an example:

@Component
public class AccountCommandHandlers {

    private Repository<Account> accountRepository;
    private AccountQueryRepository queryRepository;

    @Autowired
    public AccountCommandHandlers(Repository<Account> accountRepository, AccountQueryRepository queryRepository) {
        this.accountRepository = accountRepository;
        this.queryRepository = queryRepository;
    }

    @CommandHandler
    public void handle(CreateAccountCommand command) throws Exception {
        accountRepository.newInstance(() -> new Account(command.getAccountId(),
                command.getFirstName(), command.getLastName(), command.getEmail(),
                command.getPhone(), command.getCompanyNumber(), command.getType()));

// adding this code make stops saving data in the domain_entry table
// I did that in the event hanlder as well without luck.
        AccountEntity accountEntity = new AccountEntity();

        accountEntity.setCompanyNumber(command.getCompanyNumber());
        accountEntity.setEmail(command.getEmail());
        accountEntity.setFirstName(command.getFirstName());
        accountEntity.setLastName(command.getLastName());
        accountEntity.setPhone(command.getPhone());
        accountEntity.setType(command.getType());
        queryRepository.save(accountEntity); 
    }
}

Hi,

In your query side you will have only EventHandler no commands, so you remove queryRepository from AccountCommandHandlers
and create a new Class XxxxEventHandler with a @EventHandler annotated method, inside it you call your QueryRepository to save a new Object built with the data carried in the Event. The whole idea is to separate the write side accountRepository
and your querySide queryRepository

Thanks Master MInd

Now I am getting the following error:

Here is my config for Axon 2.4.5. you can use it as an example

For Axon 3 you can use @EnableAxon annotation which will create a default configuration, you can override the component you want by using using your own configuration class like for Axon 2.

AxonConf

@Configuration
public class AxonConfiguration {

    @Autowired
    Mongo mongo;

    String databaseName = "cqrs";
    String eventsCollection = "events";
    String snapshotsCollection = "snapshots";

    @Bean
    JacksonSerializer axonJsonSerializer() {
        return new JacksonSerializer();
    }

    @Bean
    MongoEventStore mongoEventStore() {
        MongoEventStore eventStore = new MongoEventStore(axonJsonSerializer(), mongoTemplate());
        return eventStore;
    }

    public DefaultMongoTemplate mongoTemplate() {
        return new DefaultMongoTemplate(mongo,
                databaseName, eventsCollection, snapshotsCollection, null, null);
    }

    @Bean
    public SnapshotEventStore snapshotEventStore() {
        return mongoEventStore();
    }

    @Bean
    public EventStore eventStore() {
        return mongoEventStore();
    }

    @Bean
    EventSourcingRepository<Post> postEventSourcingRepository() {
        EventSourcingRepository<Post> repo = new EventSourcingRepository<Post>(Post.class, eventStore());
        repo.setEventBus(eventBus());
        return repo;
    }

    @Bean
    CommandBus commandBus() {
        SimpleCommandBus commandBus = new SimpleCommandBus();
        return commandBus;
    }

    @Bean
    CommandGatewayFactoryBean<CommandGateway> commandGatewayFactoryBean() {
        CommandGatewayFactoryBean<CommandGateway> factory = new CommandGatewayFactoryBean<CommandGateway>();
        factory.setCommandBus(commandBus());
        return factory;
    }

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(5);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        return executor;
    }

    @Bean
    public Snapshotter snapshotter() {
        SpringAggregateSnapshotter snapshotter = new SpringAggregateSnapshotter();
        snapshotter.setExecutor(taskExecutor());
        snapshotter.setEventStore(snapshotEventStore());
        return snapshotter;
    }

    @Bean
    public SnapshotterTrigger snapshottrigger(Snapshotter snapshotter) {
        EventCountSnapshotterTrigger snapshotterTrigger = new EventCountSnapshotterTrigger();
        snapshotterTrigger.setTrigger(5);
        snapshotterTrigger.setSnapshotter(snapshotter);
        return snapshotterTrigger;
    }

    @Bean
    AnnotationEventListenerBeanPostProcessor eventListenerBeanPostProcessor() {
        AnnotationEventListenerBeanPostProcessor proc = new AnnotationEventListenerBeanPostProcessor();
        proc.setEventBus(eventBus());
        return proc;
    }

    @Bean
    AnnotationCommandHandlerBeanPostProcessor commandHandlerBeanPostProcessor() {
        AnnotationCommandHandlerBeanPostProcessor proc = new AnnotationCommandHandlerBeanPostProcessor();
        proc.setCommandBus(commandBus());
        return proc;
    }

    @Bean
    EventBus eventBus() {
        return new SimpleEventBus();
    }


SagaConf if you want to use Saga it extends the AxonConf

String sagaCollection = "sagas";

@Autowired
private ResourceLoader resourceLoader;

@Bean
public SagaRepository sagaRepository() {
    MongoTemplate template = new DefaultMongoTemplate(mongo, databaseName, sagaCollection, null, null);
    MongoSagaRepository rep = new MongoSagaRepository(template);
    rep.setResourceInjector(resourceInjector());
    return rep;
}

@Bean
public AnnotatedSagaManager sagaManager(SagaRepository sagaRepository, EventBus eventBus) {
    final AnnotatedSagaManager annotatedSagaManager = new AnnotatedSagaManager(
            sagaRepository, sagaFactory(),
            PostStatusSaga.class
    );
    eventBus.subscribe(annotatedSagaManager);
    return annotatedSagaManager;
}

@Bean
public SagaFactory sagaFactory() {
    final GenericSagaFactory sagaFactory = new GenericSagaFactory();
    sagaFactory.setResourceInjector(resourceInjector());
    return sagaFactory;
}

@Bean
public EventScheduler eventScheduler(EventBus eventBus, SchedulerFactoryBean schedulerFactoryBean) {
    final QuartzEventScheduler quartzEventScheduler = new QuartzEventScheduler();
    quartzEventScheduler.setEventBus(eventBus);
    quartzEventScheduler.setScheduler(schedulerFactoryBean.getScheduler());

    return quartzEventScheduler;
}

@Bean
public ResourceInjector resourceInjector() {
    return new SpringResourceInjector();
}

@Bean
public SchedulerFactoryBean eventSchedulerFactoryBean() {
    SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
    factoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));
    return factoryBean;
}