-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Hibernate JPA: org.hibernate.LazyInitializationException
PostPosted: Mon Oct 25, 2010 5:22 pm 
Newbie

Joined: Mon Oct 25, 2010 5:11 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: Hibernate JPA: org.hibernate.LazyInitializationException
PostPosted: Tue Oct 26, 2010 11:53 am 
Senior
Senior

Joined: Fri Oct 08, 2010 8:44 am
Posts: 130
rajpatel84 wrote:
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.


Java passes value of the pointer. So if you pass the object then it is passed as is without copying. That means that Java parameter passing has nothing to do with your problems. I am too lazy too look what is your real problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.