title: failed to lazily initialize a collection of role: website.User.purchasedProducts, could not initialize proxy - no Session
body: <p>I'm trying to setup a tag lib to check if a user purchased a product. However, when my taglib is run, I get this error:</p>
  
  <pre><code>ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[grailsDispatcherServlet] - Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.grails.gsp.GroovyPagesException: Error processing GroovyPageView: [views/derbypro/index.gsp:124] Error executing tag &lt;g:ifPurchased&gt;: failed to lazily initialize a collection of role: website.User.purchasedProducts, could not initialize proxy - no Session] with root cause
  org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: website.User.purchasedProducts, could not initialize proxy - no Session
  </code></pre>
  
  <p>Here is my tag lib:</p>
  
  <pre><code>package website
  
  class PurchasedProductTagLib {
      def ifPurchased = { attrs, body -&gt;
          if (!session.user) return
  
          if (Product.findById(attrs.product) in session.user.purchasedProducts) { // &lt;-- error here
              out &lt;&lt; body()
          }
      }
  
      def ifNotPurchased = { attrs, body -&gt;
          if (!(Product.findById(attrs.product) in session.user?.purchasedProducts)) {
              out &lt;&lt; body()
          }
      }
  }
  </code></pre>
  
  <p>And here is my User domain class:</p>
  
  <pre><code>package website
  
  import org.mindrot.jbcrypt.BCrypt
  
  class User {
      String username
      String passwordHash
      String email
  
      static hasMany = [purchasedProducts: Product]
  
      User(String username, String password, String email) {
          this.username = username;
          passwordHash = BCrypt.hashpw(password, BCrypt.gensalt())
          this.email = email
      }
  }
  </code></pre>
  
  <p>This only seems to be happening after logging in, if the user registers instead (and is redirected back to this page), this error does not happen.</p>
  
  <p>I have my tag libs nested inside one-another, if that does anything.</p>