title: Criteria API join a lazy Collection
body: <p>I have the following setup</p>
  
  <pre><code>@Entity
  @Table(name = "Product")
  public class Product implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
  
      private String name;
  
      @OneToMany(cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
      private final List&lt;Item&gt; itemlist = new ArrayList&lt;Item&gt;();
  
      //some other attributes, getter/setter
  }
  
  @Entity
  @Table(name = "Item")
  public class Item implements Serializable {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
  
      private Date startDate;
  
      //some other attributes, getter/setter
  }
  </code></pre>
  
  <p>The connection beween these classes is only unidirectional. If bidirectional connection is better (e.g. in terms of performance)?</p>
  
  <p>How can i query all Items which were launched after a date (startDate) and to a specific product are assigned?</p>
  
  <p>I try to implementing this with criteria api:</p>
  
  <pre><code>CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
  CriteriaQuery&lt;Item&gt; criteriaQuery = criteriaBuilder.createQuery(Item.class);
  criteriaQuery = criteriaQuery.distinct(true);
  
  Metamodel m = em.getMetamodel();
  EntityType&lt;Product&gt; Product_ = m.entity(Product.class);
  Root&lt;Product&gt; root = criteriaQuery.from(Product_);
  Join join = root.join("itemlist");
  
  
  Predicate condition = criteriaBuilder.equal(join.get("product"), selectedProduct);
  criteriaQuery.where(condition);
  
  criteriaQuery.select(join);
  
  TypedQuery&lt;Item&gt; tq = em.createQuery(criteriaQuery);
  System.out.println("result " + tq.getResultList());
  </code></pre>
  
  <p>I got the exception:</p>
  
  <p>org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.test.Product.itemlist, no session or session was closed</p>
  
  <p>Is there a problem with my Query or with the lazy itemlist?</p>