-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: EntityGraphHint causes HQLQueryPlan to be recalculated
PostPosted: Mon Feb 22, 2016 5:58 am 
Newbie

Joined: Mon Feb 22, 2016 5:41 am
Posts: 3
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é.


Top
 Profile  
 
 Post subject: Re: EntityGraphHint causes HQLQueryPlan to be recalculated
PostPosted: Mon Feb 22, 2016 10:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
But the hint is applied dynamically so Hibernate cannot use the pre-build cache plan since the fetch strategy is overridden at runtime.
The fetch graph is also the only fetching strategy that allows overriding the EAGER fetching policy and the data access code can dynamically change the hint at runtime.


Top
 Profile  
 
 Post subject: Re: EntityGraphHint causes HQLQueryPlan to be recalculated
PostPosted: Mon Feb 22, 2016 10:45 am 
Newbie

Joined: Mon Feb 22, 2016 5:41 am
Posts: 3
Sure, if the hint is provided at runtime, I can see your point.
But what if both the named query and the (always) desired entitygraph for this named query are known and fixed at deploy-time?
After all, named queries are designed / meant to be processed at deploy time, and by setting an entity graph hint at runtime, this advantage over 'normal' queries seems to be minimized, if any!?
I've tried, but to no avail...:

Code:
@NamedEntityGraph(name = "InlegJpa", includeAllAttributes = true)
@NamedQueries({
        @NamedQuery(name = "InlegJpa.queryInleggenMetPeriodiekOpbouwVoorKlant",
                query = "SELECT DISTINCT inleg FROM InlegJpa as inleg, "
                        + "InlegBetaalherkomstJpa as herkomst "
                        + "WHERE inleg.pk.verzekerdeIdentificatie = herkomst.pk.klantnummerInleg "
                        + "AND inleg.pk.sppProductIdentificatie = herkomst.pk.sppIdInleg "
                        + "AND inleg.pk.polisOnderdeelNummer = herkomst.pk.polisonderdeelNummerInleg "
                        + "AND inleg.pk.keynummer = herkomst.pk.keynummerInleg "
                        + "AND inleg.pk.verzekerdeIdentificatie = :persoonId "
                        + "AND inleg.datumVerificatie IS NOT NULL "
                        + "AND inleg.datumEindeRegistratie IS NULL",
                    hints = {
                        @QueryHint(name = "javax.persistence.loadgraph", value = "InlegJpa"),
                        @QueryHint(name = "org.hibernate.readOnly", value = "true")
    }),
...


Debugging into Hibernate code, I found that the QueryHint on readonly gets processed and set to true, but the loadgraph hint doesn't.
Moreover, the org.hibernate.engine.spi.NamedQueryDefinition class doesn't have an attribute to hold a reference to an entity graph instance.

So, can it be done, is it a design flaw, or am I lacking some knowledge still?

David.


Top
 Profile  
 
 Post subject: Re: EntityGraphHint causes HQLQueryPlan to be recalculated
PostPosted: Mon Feb 22, 2016 11:21 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
I think it's a feature that hasn't yet been implemented. I'd suggest you add a JIRA issue for this.


Top
 Profile  
 
 Post subject: Re: EntityGraphHint causes HQLQueryPlan to be recalculated
PostPosted: Wed Feb 24, 2016 9:43 am 
Newbie

Joined: Mon Feb 22, 2016 5:41 am
Posts: 3
Thanx, I've done just that!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.