Spring Data JPA - Infinite loop when updating, but not saving an auditable object
I'm using hades 2.0.3 and hibernate 3.6.6. I've got a uni directional relationship from an entity that extends AbstractAuditable to my user entity.
The behavior I'm seeing is that on @PrePersist everything works fine. But on @PreUpdate, the AuditingEntityListener gets stuck in an infinite loop until I get a StackOverFlow. Specifically, the act of retrieving the user from the userDAO triggers the @PreUpdate. The userDAO is just an interface extending GenericDAO
Below is my auditaware implementation:
Code:
public class AuditorAwareBean implements AuditorAware<User> {
//~ Instance fields ----------------------------------------------------------------------------
@Resource private UserDAO UserDAO;
//~ Methods ------------------------------------------------------------------------------------
/** @see org.synyx.hades.domain.auditing.AuditorAware#getCurrentAuditor() */
@Override public User getCurrentAuditor() {
String id = SecurityContextHolder.getContext().getAuthentication()
.getName()
;
// This sets off an infinte loop during update only
return userDAO.findByUserId(id);
}
}
I have the same problem but..
Hi,
I got the sampe problem yesterday in my project when I use a declared query:
Code:
public SystemUser getCurrentAuditor() {
SecurityContext secureContext = SecurityContextHolder.getContext();
Authentication authentication = secureContext.getAuthentication();
Object principal = authentication.getPrincipal();
UserDetails userDetails = (UserDetails) principal;
SystemUser systemUser = systemUserDAO.findByUsername(userDetails.getUsername());
return systemUser;
}
But when I changed it to findOne from the interface JPARepository I no longer got the StackOverflow
Code:
public SystemUser getCurrentAuditor() {
SecurityContext secureContext = SecurityContextHolder.getContext();
Authentication authentication = secureContext.getAuthentication();
Object principal = authentication.getPrincipal();
UserDetails userDetails = (UserDetails) principal;
SystemUser systemUser = systemUserDAO.findOne(1);
return systemUser;
}
Hope this helps in some way, I've tried both 1.1.0.M1 and 1.0.1.RELEASE
//Evo 1
I'm also seeing this problem
Hi Oliver,
Has any further investigation been made into this issue? I came across it yesterday and have had a identical experience to the other guys here.
This problem is consistent but it doesn't happen every time I try to persist an Auditable entity. In my situation, the problem is triggered based on additional entity relationships - for example, my Auditable entity has a @ManyToMany relationship with another entity, but the recursive loop only arises when a join is present. If no join is present, the persist does not enter the recursive loop.
I'm going to try with one of the workarounds suggested but I do believe this warrants further investigation because it seems pretty convincing to me that this a bug in Spring Data JPA - most likely in the area of dynamic finders.
Thanks for any time you can spend on looking into this.
Cheers,
Andrew