1 Reply Latest reply on Dec 20, 2007 11:17 AM by Pete Muir

    Updating a many to many relationship from the inverse side

    Radu Ometita Newbie

      Hi.

      I have been trying to update a many to many realationship from the inverse entity and i am getting a 'org.hibernate.LazyInitializationException: illegal access to loading collection'.

       @ManyToMany(mappedBy = "fleets", fetch = FetchType.EAGER)
       public List<User> getUsers() {
       return users;
       }
      
       public void setUsers(List<User> users) {
       // Add users to relation
       if (users != null) {
       for (User u : users) {
       u.getFleets().add(this);
       }
       }
      
       this.users = users;
       }
      


      The offending line is u.getFleets().add(this). The user fleets seems to be lazy loaded (i specified EAGER loading on the User entity class) and the Hibernate session discarded.. However i don't know why would the hibernate session be discarded.

      Doing the same with many to one and one to one relationships worked fine.

      Also pointers on how else i could be doing this, or pointers to further documentation will be largely apreciated.

      Thank you,
      Adrian.