So i'm getting the classic org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: , no session or session was closed exception
I'm getting in this class when trying to get a user's roles (in the for loop in getAuthorities function):
Code:
public class PDBuildUserDetailsService implements UserDetailsService {
private static Logger log = Logger.getLogger(PDBuildUserDetailsService.class);
@Autowired
public void setUserController(UserController uc) {
this.uc = uc;
}
private UserController uc;
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
PDUser user = uc.findActivateUserByUserId(username);
if (user == null) {
log.debug("User '" + username + "' was not found or not active");
throw new UsernameNotFoundException(username + " not found or not ACTIVE");
} else
log.debug("Retreiving user '" + user.getUserId() + "' with attributes enabled="+user.getEnabled()
+", locked=" +user.getLocked() + ", bad_login_attemps=" +user.getBadLoginAttempts()
+ ", activated=" +user.getActivated());
User userDetails = new User(user.getUserId(), user.getPassword(), user.getEnabled(), true, true, !user.getLocked(), getAuthorities(user));
return userDetails;
}
private Collection getAuthorities(PDUser user) {
Set<GrantedAuthority> authList = new HashSet<GrantedAuthority>();
//Retrieve user roles --- ALL ACTIVATED USERS MUST HAVE AT LEAST ONE ROLE!!
for (UserRole userRole = user.getUserRoleCollection()) {
String role = userRole.getRole().getName().toUpperCase();
authList.add(new GrantedAuthorityImpl("ROLE_" + role));
}
return authList;
}
}
Does the object become detached if I'm passing it to another function? I know java is passing it by value so just wondering if it's now detached.
Any help appreciated.
Thanks,
Raj