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:
[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;
}
where BaseEntity has the ID and auditing info.
I am making a PUT/PATCH request to the following path:
with the body payload as
{ "name": "new name" }
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?
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);
}
to:
@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);
}
and everything works as expected.
来源:https://stackoverflow.com/questions/53661587/javax-persistence-rollbackexception-error-while-committing-the-transaction-wit