我想在我的项目中使用Envers。最难的部分是创建审计表。数据库结构由flyway迁移创建。因此hibernate.hbm2ddl.auto = validate
。根据文档,我应该使用org.hibernate.envers.tools.hbm2ddl.EnversSchemaGenerator
以编程方式创建模式。但我没有找到任何例子如何做到这一点。
我为审计表创建了flyway迁移。然而,似乎envers在dev conf开始时找不到它们。测试conf工作得很好。这是我的dev.的application.yml:
spring.datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/server?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&connectionCollation=utf8&characterSetResults=UTF-8
username: root
spring.jpa:
database: MYSQL
show-sql: true
hibernate:
ddl-auto: validate
dialect: org.hibernate.dialect.MySQL5Dialect
naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties.org.hibernate.envers:
default_schema: server_audit
audit_table_suffix: _history
和测试:
spring:
profiles: test
spring.datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:mem:test;MODE=MySQL;INIT=CREATE SCHEMA IF NOT EXISTS \"public\"\;CREATE SCHEMA IF NOT EXISTS \"SERVER_AUDIT\"
spring.jpa:
database: H2
show-sql: true
hibernate:
ddl-auto: validate
dialect: org.hibernate.dialect.H2Dialect
naming_strategy: org.hibernate.cfg.ImprovedNamingStrategy
在开始时有一个例外:
java.sql.DatabaseMetaData : HHH000262: Table not found: accounts_history
[lication.main()] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource
[org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table: accounts_history
然后我改变hbm2ddl.auto = update
输出很奇怪:
java.sql.DatabaseMetaData : HHH000262: Table not found: accounts_history
org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000388: Unsuccessful: create table server_audit.accounts_history
org.hibernate.tool.hbm2ddl.SchemaUpdate : Table 'accounts_history' already exists
然后开始。
更新 任何人都可以提供如何使用
创建审计表的示例org.hibernate.envers.tools.hbm2ddl.EnversSchemaGenerator