我有一个使用 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
发布评论