Java Reference
In-Depth Information
@Basic(optional = false)
private String id;
private String name;
private Integer nbrlevel;
private String phone;
@OneToMany(mappedBy = "depart",fetch =
FetchType.LAZY)
private List<Student> students;
/*getter and setter*/
}
Once defined, we need to retrieve our named entity graph using the
getEntityGraph() method of EntityManager in order to use it as a property
when searching with the find method or as a query hint with a query. After executing
the following code, you will notice that in the first search, the students attribute will
not be loaded while in the second search it will be.
The following code is an example of using a named entity graph:
EntityManager em = emf.createEntityManager();
//create entity manager
PersistenceUnitUtil pUtil =
emf.getPersistenceUnitUtil();
Department depart = (Department)
em.createQuery("Select e from Department e")
.getResultList().get(0);
System.out.println("students Was loaded ?
"+pUtil.isLoaded(depart, "students"));
EntityGraph includeThis =
em.getEntityGraph("includeThis");
depart = (Department) em.createQuery("Select e
from Department e")
.setHint("javax.persistence.fetchgraph",
includeThis)
Search WWH ::




Custom Search