当前位置: 首页 > 知识库问答 >
问题:

审计Spring Boot数据jpa Hibernate jpa 2事务异常

昝涛
2023-03-14

我挣扎了两天试图解决这个问题,我不知道我还能做什么。在我将< code>@Audit包含在实体中之前,我的系统工作得非常好。当然,我在。pom文件。

<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-envers</artifactId>
</dependency>

我已经尝试过这些帖子:

Hibernare envers Audit with Spring data JPA 和 Spring Boot

Spring-data-enverHibernatejava.lang.NoSuch方法错误:org.hibernate.engine.spi.SessionImplementor.getTransaction协调器

Hibernate环境在使用 CrudRepository 删除具有集合的实体时引发异常

但不幸的是,没有成功:(

我检查了我的maven存储库,发现它使用的是hibernate5.0.12.最终版本的hibernate内核、hibernate实体管理器和hibernate envers,无论我在.pom文件中设置了什么版本。

这是我的. pom文件的依赖项:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
  </dependency>
  <dependency>
    <groupId>org.hibernate.javax.persistence</groupId>
    <artifactId>hibernate-jpa-2.1-api</artifactId>
    <version>1.0.0.Final</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>4.2.3.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>4.2.3.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>io.jsonwebtoken</groupId>
    <artifactId>jjwt</artifactId>
    <version>0.7.0</version>
  </dependency>
  <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.1.3</version>
  </dependency>
  <dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>2.6.1</version><!--$NO-MVN-MAN-VER$-->
  </dependency>

  <dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-core</artifactId>
    <version>1.5.13</version>
  </dependency>

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.springframework.restdocs</groupId>
    <artifactId>spring-restdocs-mockmvc</artifactId>
    <scope>test</scope>
  </dependency>

  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
  </dependency>
</dependencies>

这是侦听器类

public class BRAuditEnversListener implements RevisionListener {
  protected static final Logger log = Logger.getLogger("com.zerofila.web");

  @Autowired
  private UserService service;

  @Override
  public void newRevision(Object revisionEntity) {        
    BRAuditRevisionEntity customRevisionEntity = (BRAuditRevisionEntity) revisionEntity;
    customRevisionEntity.setUsername( service.getAuthenticatedUser().getEmail() );
  }
}

Revinfo 类:

@Entity
@Table(name = "revinfo")
@RevisionEntity(BRAuditEnversListener.class)
public class BRAuditRevisionEntity implements Serializable {
  private static final long serialVersionUID = 1L;

  @RevisionNumber
  private int id;
  @RevisionTimestamp
  private long timestamp;
  private String username;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  @Transient
  public Date getRevisionDate() {
    return new Date(timestamp);
  }

  public long getTimestamp() {
    return timestamp;
  }

  public void setTimestamp(long timestamp) {
    this.timestamp = timestamp;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (!(o instanceof DefaultRevisionEntity)) {
        return false;
    }

    DefaultRevisionEntity that = (DefaultRevisionEntity) o;

    if (id != that.getId()) {
        return false;
    }
    if (timestamp != that.getTimestamp()) {
        return false;
    }

    return true;
  }

  @Override
  public int hashCode() {
    int result;
    result = id;
    result = 31 * result + (int) (timestamp ^ (timestamp >>> 32));
    return result;
  }

  @Override
  public String toString() {
    return "DefaultRevisionEntity(id = " + id + ", revisionDate = " + DateFormat.getDateTimeInstance().format(getRevisionDate()) + ")";
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }
}

调用插入方法的Web服务endpoint:

    @Override
@RequestMapping(value = "/create", method = RequestMethod.POST)
public ResponseEntity<Local> create(@ApiParam(value = "Local json stream resource", required = true) @Valid @RequestBody Local local) {
    Local created = service.insert(local);

    if (null == created)
        return new ResponseEntity<>(HttpStatus.NOT_MODIFIED);

    return new ResponseEntity<Local>(created, HttpStatus.CREATED);
}

这里有一个抛出异常的方法:

@Transactional
@Override
public Local insert(Local local) {
    return repository.save( local );
}

最后,例外(非常短,顺便说一句):

2017-08-26 20:18:52.006 ERROR 2784 --- [nio-8080-exec-4] org.hibernate.AssertionFailure           : HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException
2017-08-26 20:18:52.009  WARN 2784 --- [nio-8080-exec-4] .m.m.a.ExceptionHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction

我需要配置一些特殊功能吗?也许可以添加一些注释?知道我能做什么吗?

共有1个答案

耿建弼
2023-03-14

经过长时间(大约3天)的尝试、测试和分析所有的源代码,我发现了错误,从而找到了解决方案。

由于此条目而发生错误:

@Autowired
private UserService service;

由于某种原因,service.get的身份验证用户()返回null,然后抛出异常。

我的解决方案是直接从SecurityContextHolder获取经过身份验证的用户,如下所示:

public class BRAuditEnversListener implements RevisionListener {

    @Override
    public void newRevision(Object revisionEntity) {        
        BRAuditRevisionEntity customRevisionEntity = (BRAuditRevisionEntity) revisionEntity;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        customRevisionEntity.setUsername( (String)authentication.getPrincipal() );
    }
}

现在,事情正在进行:)

 类似资料:
  • 2018-02-28 13:18:20.062警告15208--[restartedMain]ationConfigEmbeddedWebApplicationContext:上下文初始化过程中遇到异常-取消刷新尝试:org.springFramework.Beans.Factor.UnsatistifiedDependencyException:创建类路径资源[org/springFramewo

  • 以下是我的要求: 异步日志记录-最小化性能影响 将审核数据存储在不同的数据库中-性能原因也是 就我所见,JaVers并不是为上述目的而设计的,但似乎可以适应实现上述目的。方法如下: null null null 由于不在同一事务中执行审计,就像事务失败一样,这会使审计回滚变得复杂。因此,我们只需要审计成功提交的对象。我打算通过使用Hibernate拦截器来实现这一点,监听afterTransact

  • 我有一组用于应用程序配置的域对象及其相关表。经过验证的用户可以通过表示层更改这些域对象数据。这些域对象有非常重要的数据,我需要找到谁和何时改变了他们的数据。我的应用程序的数据访问层是使用JPA、Hibernate和Spring实现的。我需要有每个变化的记录,包括:用户操作日期操作类型以前的值。 例如,让我们考虑一个简单的域对象(简化为这个问题的目的): 假设存在具有以下值的实例: 用户(如John

  • 原作 Tom Rhodes 和 Robert Watson. 18.1. 概述 FreeBSD 中包含了对于细粒度安全事件审计的支持。事件审计能够支持可靠的、 细粒度且可配置的, 对于各类与安全有关的系统事件,包括登录、 配置变更, 以及文件和网络访问等的日志记录。这些日志记录对于在正在运行的系统上实施监控、入侵检测和事后分析都十分重要。 FreeBSD 实现了 Sun 所发布的BSM API 和

  • 一旦Spring Security发挥作用,Spring Boot Actuator就有一个灵活的审计框架,可以发布事件(默认情况下,“身份验证成功”,“失败”和“访问被拒绝”例外)。 此功能对于报告和基于身份验证失败实施锁定策略非常有用。 要自定义已发布的安全事件,您可以提供自己的AbstractAuthenticationAuditListener和AbstractAuthorizationA

  • 问题内容: 我的问题是:是否有我可以使用的参考,也许是一本书或诸如决策树之类的东西,我可以参考这些参考来基于一些输入变量来决定应该走的路,例如: 数据库架构的成熟度 如何查询日志 需要重新创建记录的概率 更重要的是:写入或读取性能 所记录的值的性质(字符串,数字,blob) 可用的存储空间 我知道的方法是: 1.添加创建和修改日期及用户的列 表格示例: id value_1 value_2 val