Envers, Liquibase и Hibernate
Я ищу способ создания (с DDL = нет в спящем режиме) схемы для таблиц аудита на лету.
На данный момент я использую liquibase для создания схемы моих таблиц. Конфигурация выполняется с помощью hibernate (приложение с весенней загрузкой) с базой данных DB2. На данный момент у меня есть ошибки с оператором вставки для таблиц 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
Я читал в Hibernate 4 с плагином Hibernate в liquibase, можно создавать таблицы без какой-либо специальной конфигурации, за исключением тега. <referenceUrl>
в 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>
EntityBean
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;
}
application.properties
# ===============================
# = 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)