em.close();
}
public void testCriteriaAPIOneFetch() {
EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
// 6.5.4 Fetch Joins
// Example:
// CriteriaQuery<Department> q = cb.createQuery(Department.class);
// Root<Department> d = q.from(Department.class);
// d.fetch(Department_.employees, JoinType.LEFT);
// q.where(cb.equal(d.get(Department_.deptno), 1)).select(d);
//
// This query is equivalent to the following Java Persistence query
// language query:
// SELECT DISTINCT d
// FROM Department d LEFT JOIN FETCH d.employees
// WHERE d.deptno = 1
CriteriaQuery<Department> q = cb.createQuery(Department.class);
Root<Department> d = q.from(Department.class);
d.fetch(Department_.employees, JoinType.LEFT);
q.where(cb.equal(d.get(Department_.deptno), 20)).select(d).distinct(true);
List<Department> ds = em.createQuery(q).getResultList();
System.out.println("-- testCriteriaAPIOneFetch -----");
em.clear();
Assert.assertEquals(1, ds.size());