title: JSF and JPA with @ManyToMany and custom converter: LazyInitializationException
body: <p>I have an <code>@Entity</code> with :</p>
  
  <pre><code>@Entity
  public class Issue implements Serializable
  {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
   protected long id; 
  
   @ManyToOne
   private IssueScope scope;
  
    //getter/setter
  }
  </code></pre>
  
  <p>I use the custom <code>IssueScopeConverter</code> to directly use the <code>IssueScope</code> with <em>f:selectItems</em>. The converter simply</p>
  
  <ul>
  <li>returns the <em>id</em> for the method <code>getAsString</code></li>
  <li>return a newly created object <code>IssueScope</code> (with <em>id</em> set) for <code>getAsObject</code></li>
  </ul>
  
  <p>This does not raise any problems (and is used many times before with <code>@ManyToOne</code>) with <em>p:selectOneMenu</em> and similar components with code like this:</p>
  
  <pre><code>&lt;h:form id="fScope"&gt;
    &lt;p:selectOneButton rendered="true" value="#{issueBean.issue.scope}"
                       converter="IssueScopeConverter"&gt;  
      &lt;f:selectItems value="#{issueBean.issueScopes}" var="s"
                     itemLabel="#{s.name}" itemValue="#{s}"/&gt;
    &lt;/p:selectOneButton&gt;
    &lt;p:commandButton value="Save" actionListener="#{issueBean.save()}"/&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>Now let's describe my problem: In fact I don't need a <code>@ManyToOne</code>, I need a <code>@ManyToMany</code> relationship from <code>Issue</code> to <code>IssueScope</code>:</p>
  
  <pre><code>@ManyToMany(fetch=FetchType.EAGER)
  private List&lt;IssueScope&gt; scopes;
  </code></pre>
  
  <p>and the XHTML will change to this:</p>
  
  <pre><code>&lt;h:form id="fScopes"&gt;
    &lt;p:selectManyCheckbox value="#{issueBean.issue.scopes}"
                          converter="ErpIssueScopeConverter"&gt;  
      &lt;f:selectItems value="#{issueBean.issueScopes}" var="s"
                     itemLabel="#{s.name}" itemValue="#{s}"/&gt;
    &lt;/p:selectManyCheckbox&gt;
    &lt;p:commandButton value="Save" actionListener="#{issueBean.save()}"/&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>If I newly create <code>Issue</code> and then push the <em>Save</em> button to persist the entity this is done without exception. Even selected <code>IssueScopes</code> are persisted. Then, if I want to update the entity, I get a <code>failed to lazily initialize a collection, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed</code> after pushing the button.</p>
  
  <p>The method <code>public void save()</code> in my <code>@Named @ViewScoped IssueBean</code> is never entered.</p>
  
  <p>The problem seems to be related to <a href="https://stackoverflow.com/questions/9886390/lazy-loading-exception-when-using-jsf-converter-refering-to-a-collection">Lazy loading exception when using JSF Converter (refering to a collection)</a>, but I don't use Seam persistence or have special kind of TransactionInterceptor`. </p>