我有一个使用 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 请求:
主体负载为
{ "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/