コード例 #1
0
  @Test
  public void testFetchJoin2() {
    log.info("** testFetchJoin2() ***");

    EntityManager em2 = createEm();
    CriteriaBuilder cb = em2.getCriteriaBuilder();
    CriteriaQuery<Store> qdef = cb.createQuery(Store.class);

    // select s from Store s JOIN FETCH s.sales
    // where s.name='Big Al''s'
    Root<Store> s = qdef.from(Store.class);
    s.fetch("sales");
    qdef.select(s).where(cb.equal(s.get("name"), "Big Al's"));

    Store store = em2.createQuery(qdef).getSingleResult();
    log.info("em.contains(" + em2.contains(store) + ")");
    em2.close();
    store.getSales().get(0).getAmount();
  }
コード例 #2
0
  /**
   * This test demonstrates the function of a JOIN FETCH to perform the EAGER retrieval of entities
   * as a side-effect of the query
   */
  @Test
  public void testFetchJoin1() {
    log.info("** testFetchJoin1() ***");

    EntityManager em2 = createEm();
    CriteriaBuilder cb = em2.getCriteriaBuilder();
    CriteriaQuery<Store> qdef = cb.createQuery(Store.class);

    // select s from Store s JOIN s.sales
    // where s.name='Big Al''s'
    Root<Store> s = qdef.from(Store.class);
    s.join("sales");
    qdef.select(s).where(cb.equal(s.get("name"), "Big Al's"));

    Store store = em2.createQuery(qdef).getSingleResult();
    log.info("em.contains(" + em2.contains(store) + ")");
    em2.close();
    try {
      store.getSales().get(0).getAmount();
      fail("did not trigger lazy initialization exception");
    } catch (LazyInitializationException expected) {
      log.info("caught expected exception:" + expected);
    }
  }