Axon Event Sourcing 未生成 table
Axon Event Sourcing not generating table
我正在学习如何使用 axon 框架进行事件溯源,似乎进展顺利,但我一直卡在事件溯源的数据库配置上。据我从文档/其他文章中了解到,数据库 table 应该会自动生成。
我首先尝试使用 H2,数据库 table 是自己生成的,一切正常。
我添加了自己的 mysql 数据库,但未创建数据库 table。我收到错误消息
'Table 'producttest.domain_event_entry' doesn't exist
我的印象是 table 会自行生成,我一定是在这里做错了什么,但我不确定是什么。有人可以帮帮我吗?
我的代码:
ProductAggregate class
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(), "Oreo", "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, productName=Oreo, 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
在这种情况下,您应该指导您的应用程序如何创建表。
您有 2 个选择:
- 告诉 JPA 这样做,添加
spring.jpa.hibernate.ddl-auto=create
或 spring.jpa.hibernate.ddl-auto=update
- 使用更强大的工具,例如
flyway
、liquibase
等
您显示的所有其他配置看起来都不错。
我正在学习如何使用 axon 框架进行事件溯源,似乎进展顺利,但我一直卡在事件溯源的数据库配置上。据我从文档/其他文章中了解到,数据库 table 应该会自动生成。
我首先尝试使用 H2,数据库 table 是自己生成的,一切正常。 我添加了自己的 mysql 数据库,但未创建数据库 table。我收到错误消息
'Table 'producttest.domain_event_entry' doesn't exist
我的印象是 table 会自行生成,我一定是在这里做错了什么,但我不确定是什么。有人可以帮帮我吗?
我的代码:
ProductAggregate class
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(), "Oreo", "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, productName=Oreo, 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
在这种情况下,您应该指导您的应用程序如何创建表。
您有 2 个选择:
- 告诉 JPA 这样做,添加
spring.jpa.hibernate.ddl-auto=create
或spring.jpa.hibernate.ddl-auto=update
- 使用更强大的工具,例如
flyway
、liquibase
等
您显示的所有其他配置看起来都不错。