Axon Event Sourcing 不生成表

问题描述

我正在学习如何使用 axon 框架进行事件溯源,似乎进展顺利,但我在事件溯源的数据库配置上陷入了困境。从我从文档/其他文章中了解到,数据库表应该会自动生成

我第一次尝试使用H2,数据库表是自己生成的,一切正常。 我添加了我自己的 MysqL db,但没有创建数据库表..我收到错误

'Table 'producttest.domain_event_entry' doesn't exist

我的印象是表格会自行生成,我一定在这里做错了什么,但我不确定是什么。有人可以帮我吗?

我的代码

ProductAggregate 类

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.axonframework.commandhandling.CommandHandler;
import org.axonframework.eventsourcing.EventSourcingHandler;
import org.axonframework.modelling.command.AggregateIdentifier;
import org.axonframework.spring.stereotype.Aggregate;

import java.util.UUID;

import static org.axonframework.modelling.command.AggregateLifecycle.apply;

@NoArgsConstructor
@Aggregate
@Slf4j
public class ProductAggregate {

    @AggregateIdentifier
    private String productId;

    private String productName;
    private String productDescription;

    @CommandHandler
    public ProductAggregate(ProductCreateCommand command){
        log.info("handling {}",command);
        apply(new ProductCreatedEvent(command.getProductId(),command.getProductName(),command.getProductDescription()));
    }

    @EventSourcingHandler
    public void onProductCreateEvent(ProductCreatedEvent event){
        log.info("applying {}",event);
        this.productId = event.getProductId();
        this.productName = event.getProductName();
        this.productDescription = event.getProductDescription();
    }
}

ProductCreateCommand

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.axonframework.modelling.command.TargetAggregateIdentifier;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductCreateCommand {

    @TargetAggregateIdentifier
    private String productId;
    private String productName;
    private String productDescription;

}

ProductCreatedEvent

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.axonframework.modelling.command.TargetAggregateIdentifier;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductCreatedEvent {

    @TargetAggregateIdentifier
    private String productId;
    private String productName;
    private String productDescription;

}

测试运行器

import lombok.extern.slf4j.Slf4j;

import org.axonframework.commandhandling.gateway.CommandGateway;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@Slf4j
public class TestRunner implements CommandLineRunner {

    private final CommandGateway commandGateway;

    @Autowired
    public TestRunner(CommandGateway commandGateway) {
        this.commandGateway = commandGateway;
    }

    @Override
    public void run(String... args) throws Exception {

        log.info("sending product create command");
        commandGateway.sendAndWait(new ProductCreateCommand(UUID.randomUUID().toString(),"Oreo","biscuit"));
        log.info("sending product create command");
        commandGateway.sendAndWait(new ProductCreateCommand(UUID.randomUUID().toString(),"biscuit"));
    }
}

编辑:

application.properties 文件

logging.level.root=info
server.port=8090
spring.datasource.url=jdbc:MysqL://localhost:3306/producttest
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.use-new-id-generator-mappings= false

日志:

2021-01-06 21:44:51.229  INFO 21236 --- [           main] c.u.e.a.Client.TestRunner                : sending product create command
2021-01-06 21:44:51.257  INFO 21236 --- [           main] c.u.e.a.Aggregate.ProductAggregate       : handling ProductCreateCommand(productId=638643b7-1e4f-45b5-bfa9-1c2fd9360fa3,productName=Oreo,productDescription=biscuit)
2021-01-06 21:44:51.260  INFO 21236 --- [           main] c.u.e.a.Aggregate.ProductAggregate       : applying ProductCreatedEvent(productId=638643b7-1e4f-45b5-bfa9-1c2fd9360fa3,productDescription=biscuit)
2021-01-06 21:44:51.321  WARN 21236 --- [           main] o.h.engine.jdbc.spi.sqlExceptionHelper   : sql Error: 1146,sqlState: 42S02
2021-01-06 21:44:51.321 ERROR 21236 --- [           main] o.h.engine.jdbc.spi.sqlExceptionHelper   : Table 'producttest.domain_event_entry' doesn't exist
2021-01-06 21:44:51.333  INFO 21236 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-01-06 21:44:51.346 ERROR 21236 --- [           main] o.s.boot.SpringApplication               : Application run Failed

解决方法

在这种情况下,您应该指导您的应用程序如何创建表。

您有两个选择:

  1. 告诉 JPA 这样做,添加 spring.jpa.hibernate.ddl-auto=createspring.jpa.hibernate.ddl-auto=update
  2. 使用更强大的工具,例如 flywayliquibase

你展示的所有其他配置看起来都很好。