Hi!
I'm using a @NamedQuery on a JPA @Entity in Hibernate version 4.3.9
Now in this entity there is also a @NamedEntityGraph.
I want the two combined, so the @NamedQuery will always fetch the entity's attributes and relations eagerly, so in code I specify:
Code:
final TypedQuery<InlegJpa> inlegQuery = entityManager.getNamedEntityQuery(
"InlegJpa.queryInleggenMetPeriodiekOpbouwVoorKlant",
InlegJpa.class);
inlegQuery.setHint("javax.persistence.loadgraph", entityManager.getEntityGraph('InlegJpa"));
This piece of code is executed many times, and by debugging I learned, that the HQLQueryPlan is re-created each time,
thus not cached.
The code responsible is in the list() method of org.hibernate.jpa.internal.QueryImpl:
Code:
private List<X> list() {
if (getEntityGraphQueryHint() != null) {
SessionImplementor sessionImpl = (SessionImplementor) getEntityManager().getSession();
[b]HQLQueryPlan entityGraphQueryPlan = new HQLQueryPlan( getHibernateQuery().getQueryString(), false,
sessionImpl.getEnabledFilters(), sessionImpl.getFactory(), getEntityGraphQueryHint() );[/b]
// Safe to assume QueryImpl at this point.
unwrap( org.hibernate.internal.QueryImpl.class ).setQueryPlan( entityGraphQueryPlan );
}
return query.list();
}
This code still remains more or less the same in the current 5.1.0 version of Hibernate.
My questions are (assuming this is not a bug):
- Why isn't the HQLQueryPlan cached for the combination of a @NamedQuery and a @EntityGraph, since they both cannot change at runtime?
- How to make sure a queryplan is only calculated once for a named query, while using an entitygraph specifying what to fetch eagerly?
Thanx all for thinking along!
David Gofferjé.