How to properly initialize a lazy collection "collection is not associated with any session"
<p>I know this question has been asked a lot, but I couldn't find a way to fix my problem. So I have have a managed bean called <code>CurrentUser</code>, which holds a <code>Users</code> instance,  upon logging, I save the current logged in user in that instance, here is the login method: </p>

<pre><code>public static Users login(String username, String password) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.getTransaction().begin();

    try {
        Criteria criteria = session.createCriteria(Users.class);

        criteria.add(Restrictions.eq("username", username));
        criteria.add(Restrictions.eq("password", password));
        List&lt;Users&gt; list = criteria.list();

        try {
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        } finally {
            if (session.isOpen()) {
                session.close();
            }
        }

        if (list.isEmpty()) {
            return null;
        } else {
            new Logs(null, "Logging in", "/login.xhtml", "", list.get(0)).add();
            return list.get(0);
        }
    } catch (Exception e) {
        e.printStackTrace();
        session.getTransaction().rollback();
        if (session.isOpen()) {
            session.close();
        }
        return null;
    }
}
</code></pre>

<p>So this user have a collection of <code>expertise</code> lazily mapped:</p>

<pre><code>@OneToMany(mappedBy = "user")
@NotFound(action = NotFoundAction.IGNORE)
@Cascade({org.hibernate.annotations.CascadeType.REPLICATE, CascadeType.SAVE_UPDATE, CascadeType.MERGE})
private List&lt;Expertise&gt; expertises;
</code></pre>

<p>In another managed bean, I have injected the currentUser instance, but when I try to get the expertises, I get the <code>collection is not associated with any session</code> exception, </p>

<p>Here is my get method: </p>

<pre><code>public List&lt;Expertise&gt; getExpertises() {

    Hibernate.initialize(expertises);

    if (expertises != null) {
        return expertises.stream().distinct().collect(Collectors.toList());
    }
    return null;
}
</code></pre>

<p>and when I remove the <code>Hibernate.initialize</code>, I get the <code>org.hibernate.LazyInitializationException</code>, even thought I have <code>able_lazy_load_no_trans"&gt;true&lt;/property&gt;</code> in my config file.</p>

<p>So my question is what am I doing wrong, and how to properly initialize a lazy collection and retrieve it. Or should I just stick with eager fetching.</p>

<p>UPDATE: 
Adding <code>@LazyCollection(LazyCollectionOption.TRUE)</code> to the collections did the trick, but is it really lazy? does it really only loads the list when needed?</p>