Hibernate Envers:防止 onPostDelete 上的版本
Hibernate Envers: prevent version on onPostDelete
如果已审计实体被删除,我想阻止 Hibernate Envers 创建版本。
问题是,没有调用 Envers 的 onPostUpdate 事件,而是插入了删除版本 (REVTYPE: 2) 和更新版本 (REVTYPE: 1)。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Whosebug1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Whosebug1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
auditConfig.java:
包裹 com.example.Whosebug1.audit;
import lombok.AllArgsConstructor;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@AllArgsConstructor
public class AuditConfig {
private HibernateEntityManagerFactory hibernateEntityManagerFactory;
@PostConstruct
public void registerEnversListeners() {
EnversService enversService =
hibernateEntityManagerFactory
.getSessionFactory()
.getServiceRegistry()
.getService(EnversService.class);
EventListenerRegistry listenerRegistry = hibernateEntityManagerFactory.getSessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
listenerRegistry.setListeners(EventType.POST_INSERT, new CustomAuditEventListenerPostInsert(enversService));
listenerRegistry.setListeners(EventType.POST_DELETE, new CustomAuditEventListenerPostDelete(enversService));
}
}
customEventListenerPostDelete.java:
package com.example.Whosebug1.audit;
import com.example.Whosebug1.model.Soup;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.event.spi.EnversPostDeleteEventListenerImpl;
import org.hibernate.event.spi.PostDeleteEvent;
public class CustomAuditEventListenerPostDelete extends EnversPostDeleteEventListenerImpl {
public CustomAuditEventListenerPostDelete(EnversService enversService) {
super(enversService);
}
@Override
public void onPostDelete(PostDeleteEvent event) {
if (event.getEntity() instanceof Soup){
Soup soup = ((Soup) event.getEntity());
System.out.println("DELETE: " + soup);
}
}
}
Controller.java:
import com.sun.tools.javac.util.List;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@AllArgsConstructor
public class SoupController {
private SoupRepository soupRepository;
@RequestMapping(value = "create")
public Soup create() {
Ingredient ingredient = Ingredient.builder().name("ingredient").build();
Soup soup = Soup.builder()
.name("Soup")
.ingredients(List.of(ingredient))
.build();
return soupRepository.save(soup);
}
@RequestMapping(value = "delete")
public void delete() {
soupRepository.deleteById(1L);
}
}
在/create 之后是/delete 我想在Soup_AUD 中只有一个条目。
ID REV REVTYPE 名称
1 1 0 汤
但是我得到了:
ID REV REVTYPE 名称
1 1 0 汤
1 2 1 汤
如何避免此删除审核?
Project can be checked out -> https://github.com/MoritzMeinhardt/hibernate-envers.git
我相信您遇到这个问题是因为 Envers 如何处理将更改应用于集合的拥有实体。
您可以尝试将 org.hibernate.envers.revision_on_collection_change
设置为 false
,但请注意,这是针对所有已审计实体的,目前无法针对特定实体类型或关系。
我已登录 HHH-13819 以跟进此事。
如果已审计实体被删除,我想阻止 Hibernate Envers 创建版本。 问题是,没有调用 Envers 的 onPostUpdate 事件,而是插入了删除版本 (REVTYPE: 2) 和更新版本 (REVTYPE: 1)。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>Whosebug1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Whosebug1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
auditConfig.java: 包裹 com.example.Whosebug1.audit;
import lombok.AllArgsConstructor;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.event.service.spi.EventListenerRegistry;
import org.hibernate.event.spi.EventType;
import org.hibernate.jpa.HibernateEntityManagerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
@AllArgsConstructor
public class AuditConfig {
private HibernateEntityManagerFactory hibernateEntityManagerFactory;
@PostConstruct
public void registerEnversListeners() {
EnversService enversService =
hibernateEntityManagerFactory
.getSessionFactory()
.getServiceRegistry()
.getService(EnversService.class);
EventListenerRegistry listenerRegistry = hibernateEntityManagerFactory.getSessionFactory().getServiceRegistry().getService(EventListenerRegistry.class);
listenerRegistry.setListeners(EventType.POST_INSERT, new CustomAuditEventListenerPostInsert(enversService));
listenerRegistry.setListeners(EventType.POST_DELETE, new CustomAuditEventListenerPostDelete(enversService));
}
}
customEventListenerPostDelete.java:
package com.example.Whosebug1.audit;
import com.example.Whosebug1.model.Soup;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.event.spi.EnversPostDeleteEventListenerImpl;
import org.hibernate.event.spi.PostDeleteEvent;
public class CustomAuditEventListenerPostDelete extends EnversPostDeleteEventListenerImpl {
public CustomAuditEventListenerPostDelete(EnversService enversService) {
super(enversService);
}
@Override
public void onPostDelete(PostDeleteEvent event) {
if (event.getEntity() instanceof Soup){
Soup soup = ((Soup) event.getEntity());
System.out.println("DELETE: " + soup);
}
}
}
Controller.java:
import com.sun.tools.javac.util.List;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@AllArgsConstructor
public class SoupController {
private SoupRepository soupRepository;
@RequestMapping(value = "create")
public Soup create() {
Ingredient ingredient = Ingredient.builder().name("ingredient").build();
Soup soup = Soup.builder()
.name("Soup")
.ingredients(List.of(ingredient))
.build();
return soupRepository.save(soup);
}
@RequestMapping(value = "delete")
public void delete() {
soupRepository.deleteById(1L);
}
}
在/create 之后是/delete 我想在Soup_AUD 中只有一个条目。
ID REV REVTYPE 名称
1 1 0 汤
但是我得到了:
ID REV REVTYPE 名称
1 1 0 汤
1 2 1 汤
如何避免此删除审核?
Project can be checked out -> https://github.com/MoritzMeinhardt/hibernate-envers.git
我相信您遇到这个问题是因为 Envers 如何处理将更改应用于集合的拥有实体。
您可以尝试将 org.hibernate.envers.revision_on_collection_change
设置为 false
,但请注意,这是针对所有已审计实体的,目前无法针对特定实体类型或关系。
我已登录 HHH-13819 以跟进此事。