title: Lazy loading exception when using JSF Converter (refering to a collection)
body: <p>This is my first post after many research on this problem.</p>
  
  <p>This example is running under Jboss 7.1 with seam 3.1 (solder + persistence + faces) with seam managed persistence context</p>
  
  <p>I'm facing a problem, the classical <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> when using a converter on Entity beans. The aim is to stay 100% Object oriented, by reusing the JPA model.</p>
  
  <p>in beans.xml, <code>org.jboss.seam.transaction.TransactionInterceptor</code> is activated</p>
  
  <p>Entity beans :</p>
  
  <pre><code>@Entity
  public class Member implements Serializable {
  
      @Id
      @GeneratedValue
      private Long id;
      private String name;
      private String email;
  
      @Column(name = "phone_number")
      private String phoneNumber;
  
      @ManyToMany
      private List&lt;Statut&gt; listeStatut = new ArrayList&lt;Statut&gt;();
  
      // getters, setters, hashcode, equals
  }
  
  @Entity
  public class Statut implements Serializable {
  
      @Id
      @GeneratedValue
      private Long id;
  
      private String name;
  
      @ManyToMany(mappedBy="listeStatut")
      private List&lt;Member&gt; members = new ArrayList&lt;Member&gt;();
  
      // getters, setters, hashcode, equals
  }
  </code></pre>
  
  <p>The JSF page :</p>
  
  <pre><code>&lt;h:form&gt;
      &lt;h:selectManyCheckbox id="stat" value="#{memberModif.member.listeStatut}"&gt;
          &lt;f:converter converterId="statutConverter"/&gt;
          &lt;f:selectItems value="#{memberModif.statutsPossibles}" var="statut" itemValue="#{statut}" itemLabel="#{statut.name}" /&gt;
      &lt;/h:selectManyCheckbox&gt;
  
  
      &lt;h:commandLink id="register" action="#{memberModif.modifier()}" value="Modifier"&gt;
          &lt;f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/&gt;
      &lt;/h:commandLink&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>The backing bean (I tried with ConversationScoped after SessionScoped --> same problem)</p>
  
  <pre><code>@ConversationScoped
  @Named
  public class MemberModif implements Serializable {
  
      private static final long serialVersionUID = -291355942822086126L;
  
      @Inject
      private Logger log;
  
      @Inject
      private EntityManager em;
  
      @Inject Conversation conversation;
  
      private Member member;
  
      @SuppressWarnings("unused")
      @PostConstruct
      private void init() {
          if (conversation.isTransient()) {
              conversation.begin();
          }
      }
  
      public String modifier() {
          em.merge(member);
      }
  
      public Member getMember() {
          if (member == null) {
              member = em.createQuery("from Member m where m.id=:id",Member.class).setParameter("id", new Long(0)).getSingleResult();
          }
          return member;
      }
  
      public List&lt;Statut&gt; getStatutsPossibles() {
          return em.createQuery("from Statut", Statut.class).getResultList();
      }
  }
  </code></pre>
  
  <p>And the converter (strongly inspired by seam <code>ObjectConverter</code>) :</p>
  
  <pre><code>@FacesConverter("statutConverter")
  public class StatutConverter implements Converter, Serializable {
  
      final private Map&lt;String, Statut&gt; converterMap = new HashMap&lt;String, Statut&gt;();
      final private Map&lt;Statut, String&gt; reverseConverterMap = new HashMap&lt;Statut, String&gt;();
  
      @Inject
      private transient Conversation conversation;
  
      private final transient Logger log = Logger.getLogger(StatutConverter.class);
  
      private int incrementor = 1;
  
      @Override
      public Object getAsObject(FacesContext context, UIComponent component, String value) {
          if (this.conversation.isTransient()) {
              log.warn("Conversion attempted without a long running conversation");
          }
  
          return this.converterMap.get(value);
      }
  
      @Override
      public String getAsString(FacesContext context, UIComponent component, Object value) {
          if (this.conversation.isTransient()) {
              log.warn("Conversion attempted without a long running conversation");
          }
  
          if (this.reverseConverterMap.containsKey(value)) {
              return this.reverseConverterMap.get(value);
          } else {
              final String incrementorStringValue = String.valueOf(this.incrementor++);
              this.converterMap.put(incrementorStringValue, (Statut)value);
              this.reverseConverterMap.put( (Statut)value, incrementorStringValue);
              return incrementorStringValue;
          }
      }
  }
  </code></pre>
  
  <p>Please note that I put this converter here to avoid you searching over the net for the seam implementation, but it is the same as using <code>&lt;s:objectConverter/&gt;</code> tag instead of <code>&lt;f:converter converterId="statutConverter"/&gt;</code></p>
  
  <p>Any help would be greetly appreciated.</p>