# jpa 2.1 # This anotation allows you to change the behavior of a Lazy entity loading default, in the case the varis the atribute that # will behave like EAGUER when using a query with TypedQuery .setHint("javax.persistence.loadgraph", eg); # #theAtributeName: ... import javax.persistence.NamedAttributeNode; import javax.persistence.NamedEntityGraph; ...
@NamedEntityGraph(name="aNameToCallIt",attributeNodes={@NamedAttributeNode("theAtributeName")})
@Entity
@Table(name = "nameoftheTable")
public class ClassName implements Serializable{
...
#Using the EntityGraph attribute (with criteria)
EntityGraph<Slave> eg = (EntityGraph<Slave>) getEntityManager().getEntityGraph("slave.counterTypes");
CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Slave> criteria = cb.createQuery(Slave.class);
Root<Slave> root = criteria.from(Slave.class);
ParameterExpression<Long> param = cb.parameter(Long.class);
criteria.select(root).where(cb.equal(root.get("id"), param));
TypedQuery<Slave> q = getEntityManager().createQuery(criteria);
q.setParameter(param, id);
q.setHint("javax.persistence.loadgraph", eg);