我正在寻找一种方法来动态创建审计表的架构(在休眠中使用 DDL = none)。
目前我使用 liquibase 来创建我的表的模式。该配置使用带有 DB2 数据库的休眠(spring boot 应用程序)。目前我对 envers 表的插入语句有错误(因为不存在表):
org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [insert into revinfo (rev, revtstmp) values (default, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
<referenceUrl>
我已经阅读了带有 liquibase Hibernate 插件的 Hibernate 4,除了pom.xml中的标记外,无需任何特殊配置即可创建表
这是我的配置:
pom.xml
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<changeLogFile>src/main/resources/config/liquibase/master.xml</changeLogFile>
<diffChangeLogFile>target/${maven.build.timestamp}_liquibase_changelog.xml</diffChangeLogFile>
<driver>com.ibm.db2.jcc.DB2Driver</driver>
<url>jdbc:db2://localhost:50000/test</url>
<username>${db.username}</username>
<password>test</password>
<referenceUrl>hibernate:spring:de.test.tool.domain?dialect=org.hibernate.dialect.DB2Dialect</referenceUrl>
<verbose>true</verbose>
<logging>debug</logging>
<propertyFileWillOverride>false</propertyFileWillOverride>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<contexts>${liquibase.contexts}</contexts>
</configuration>
<dependencies>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
</dependency>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate4</artifactId>
<version>${liquibase-hibernate4.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</plugin>
实体豆
package de.test.tool.domain;
import org.hibernate.envers.Audited;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
/**
* Test-Entitaet.
*/
@Entity
@Audited
@Table(name="CALCULATEVALUE")
@EntityListeners(AuditingEntityListener.class)
public class CalculateValue {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@Column(nullable = false, name="OWN_VALUE")
private int ownValue = 0;
@Column(nullable = false, name="CALCULATION_DATE_LONG")
private long calculationDateLong = 0;
public int getOwnValue() {
return ownValue;
}
public void setOwnValue(int ownValue) {
this.ownValue = ownValue;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public long getCalculationDateLong() {
return calculationDateLong;
}
public void setCalculationDateLong(long calculationDateLong) {
this.calculationDateLong = calculationDateLong;
}
应用程序属性
# ===============================
# = JPA / HIBERNATE
# ===============================
# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager).
# Show or not log for each sql query
spring.jpa.show-sql=true
# Hibernate ddl auto (create, create-drop, update, none): with "update" the database
# schema will be automatically updated accordingly to java entities found in
# the project
spring.jpa.hibernate.ddl-auto=none
spring.jpa.generate-ddl=false
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect
# Naming strategy
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
# Connection url for the database "netgloo_blog"
spring.datasource.driverClassName=com.ibm.db2.jcc.DB2Driver
spring.datasource.url=jdbc:localhost:50000/test
# password
spring.datasource.password=test
# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT values(1)