title: Grails GORM: could not initialize proxy - no Session
body: <p>I have a method with the following structure:</p>
  
  <pre><code>public void run(){
      ... 
      for (...) { //this part works correct
  
          User.withTransaction {
              User user = User.findByUsername(...);
  
              Position pos = Position.findByName(...)
              if(pos){ ...
              } else { ...
                  try{
                      pos.save(flush:true);
                      user.position = pos;
                  } catch (Exception e){ ... }
              }
              ...
              try{
                  user.save(flush:true, failOnError: true);
              } catch (Exception e){ ... }
          }
      }
      //this part doesn't work
      User.findAll().each {
      ...
          if (...){
              User.withTransaction{
                  ...
                  //here the operation fails with 
                  //org.hibernate.LazyInitializationException: 
                  //could not initialize proxy - no Session
                  if (!userDetailsMap.containsKey(it.username) 
                           &amp;&amp; it.userStatus != blocked){
                      it.userStatus = blocked
                      it.save(flush:true) 
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>The exception I'm getting here is <code>org.hibernate.LazyInitializationException: could not initialize proxy - no Session</code> in the second part of my code. Here the <code>userStatus</code> field is a reference to a different domain class.</p>
  
  <p>I tried to add <code>it.refresh()</code> and <code>Hibernate.initialize(it)</code>to the code before properties are checked, but no use. What am I doing wrong here?</p>
  
  <p>upd: I tried to call the <code>it.attach</code> method before the properties are checked, but right after the method call the value of <code>it.attached</code> is <code>false</code>.</p>