Lazy loading: org.hibernate.LazyInitializationException

View: New views
2 Messages — Rating Filter:   Alert me  

Lazy loading: org.hibernate.LazyInitializationException

by gembin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi all,
i have some problem with Lazy Loading with hibernate

public class UserEntity{

  @ManyToMany
   @JoinTable(name="USER_ROLE",
         joinColumns=
         @JoinColumn(name="UR_USER_ID",
         referencedColumnName="USER_ID"),
         inverseJoinColumns=
         @JoinColumn(name="UR_ROLE_ID",
         referencedColumnName="ROLE_ID"))
   private Collection<RoleEntity> roles=Collections.EMPTY_LIST;
   public Collection<RoleEntity> getRoles() {
      return roles;
   }
   public void setRoles(Collection<RoleEntity> roles) {
      this.roles = roles;
   }
}

userEntity=IUserPersistenceService.findUser(1);
when i use userEntity.getRoles(); i got exception:

Exception in thread "main" org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.example.entity.UserEntity.roles, no session or session was closed

 
how to solve this problem?

Re: Lazy loading: org.hibernate.LazyInitializationException

by Florent BENOIT-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

    Hi,

You need to give more details, for example the full stack trace and
where is running your client.
Also, if you don't need lazy initialization, you should use EAGER.

Regards,

Florent

gembin wrote:

> hi all,
> i have some problem with Lazy Loading with hibernate
>
> public class UserEntity{
>
>   @ManyToMany
>    @JoinTable(name="USER_ROLE",
>          joinColumns=
>          @JoinColumn(name="UR_USER_ID",
>          referencedColumnName="USER_ID"),
>          inverseJoinColumns=
>          @JoinColumn(name="UR_ROLE_ID",
>          referencedColumnName="ROLE_ID"))
>    private Collection<RoleEntity> roles=Collections.EMPTY_LIST;
>    public Collection<RoleEntity> getRoles() {
>       return roles;
>    }
>    public void setRoles(Collection<RoleEntity> roles) {
>       this.roles = roles;
>    }
> }
>
> userEntity=IUserPersistenceService.findUser(1);
> when i use userEntity.getRoles(); i got exception:
>
> Exception in thread "main" org.hibernate.LazyInitializationException: failed
> to lazily initialize a collection of role:
> org.example.entity.UserEntity.roles, no session or session was closed
>
>  
> how to solve this problem?
>