javax.persistence.RollbackException: Error while commit the transaction] 根本原因 java.lang.StackOverflo

编程入门 行业动态 更新时间:2024-10-27 04:34:00
本文介绍了javax.persistence.RollbackException: Error while commit the transaction] 根本原因 java.lang.StackOverflowError: null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have a Spring Boot API using the Spring Data REST framework (dependencies inherited from spring-boot-starter-parent 2.1.0.RELEASE). I'm attempting to do a PUT or PATCH request to update an entity and neither seem to work, throwing the following error message:

【请求处理失败;嵌套异常是 org.springframework.transaction.TransactionSystemException: 无法提交 JPA 事务;嵌套异常是 javax.persistence.RollbackException: Error while committing the transaction] 根本原因 java.lang.StackOverflowError: null

[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

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

The entity I am trying to update has the following structure:

@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 和审核信息.

where BaseEntity has the ID and auditing info.

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

I am making a PUT/PATCH request to the following path:

localhost:8080/api/v1/entitiesA/the_uuid

主体有效载荷为

{ "name": "新名字" }

{ "name": "new name" }

由于这是一个堆栈溢出错误,我的第一个想法是递归发生了.我注释掉了 Set<EntityB > 字段(以及@OneToMany 注释),我仍然遇到错误.以前有人遇到过这个错误吗?

Since it was a stack overflow error, my first thought was something recursive is happening. I commented out the Set< EntityB > field (along with the @OneToMany annotation) and I still encountered the error. Has anyone experienced this error before?

推荐答案

问题与我实施 AuditorAware 的方式有关 界面.我使用的 userDao 方法导致递归调用.我仍然不知道为什么会这样,但是看着 本论坛,我将 getCurrentAuditor() 的实现从:

The issue was to do with the way I implemented the AuditorAware< T > interface. The userDao method I was using was causing a recursive call. I still don't know why it was happening, but looking at this forum, I changed the implementation of getCurrentAuditor() from:

@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 commit the transaction] 根本原因 ja

本文发布于:2023-09-17 06:21:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:根本原因   Error   commit   javax   persistence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!