javax.persistence.RollbackException : Error while committing the transaction] with root cause java. lang.StackOverflowError: 空

标签 java spring-data-rest

我有一个使用 Spring Data REST 框架的 Spring Boot API(从 spring-boot-starter-parent 2.1.0.RELEASE 继承的依赖项)。我正在尝试执行 PUT 或 PATCH 请求来更新实体,但似乎都不起作用,并抛出以下错误消息:

[Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction] with root cause java.lang.StackOverflowError: null

我尝试更新的实体具有以下结构:

@Getter
@Setter
@Entity
@Table(name = "entity_a")
public class EntityA extends BaseEntity {
    @Column(name = "name", nullable = false, length = 100)
    private String name

    @OneToMany(mappedBy = "entityA")
    private Set<EntityB> entitiesB;
}

其中 BaseEntity 具有 ID 和审计信息。

我正在向以下路径发出 PUT/PATCH 请求:

http://localhost:8080/api/v1/entitiesA/the_uuid

主体负载为

{ "name": "new name" }

因为这是一个堆栈溢出错误,我的第一个想法是发生了递归的事情。我注释掉了 Set< EntityB > 字段(连同 @OneToMany 注释),但我仍然遇到错误。以前有人遇到过这个错误吗?

最佳答案

问题与我实现 AuditorAware< T > 接口(interface)的方式有关。我使用的 userDao 方法导致了递归调用。我仍然不知道为什么会这样,但看看 this forum ,我更改了 getCurrentAuditor() 的实现:

@Override
public Optional<User> getCurrentAuditor() {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = userDao.findByUsername(username);
    return Optional.ofNullable(user);
}

到:

@Override
public Optional<User> getCurrentAuditor() {
    User auditor = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            auditor = (User) principal;
        }
    }
    return Optional.ofNullable(auditor);
}

一切都按预期进行。

关于javax.persistence.RollbackException : Error while committing the transaction] with root cause java. lang.StackOverflowError: 空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53661587/

相关文章:

java - 如何使用 Apache POI PPT API 设置文本字体?

spring - 将摘录投影添加到 Spring Data Rest 存储库会导致堆栈溢出错误

neo4j - 将项目添加到 HashSet<E> 时出现 NotInTransactionException

java - 无法在日期中传递空值

java - 无法在 PHP 中重现 Java MessageDigest 哈希

java - 内存泄漏——Tomcat、Spring MVC

spring - 何时使用 @RestController 与 @RepositoryRestResource

java - 如何使用包含其他资源链接的 Spring HATEOAS REST 资源?

spring-data-rest - 无法发布集合

java - ENUM 存储(内存等)