Uploaded image for project: 'Hibernate ORM'
  1. Hibernate ORM
  2. HHH-11246

Useless class lookup during constant conversion

    Details

    • Type: Improvement
    • Status: Resolved
    • Priority: Trivial
    • Resolution: Duplicate
    • Affects Version/s: 5.2.4
    • Fix Version/s: None
    • Component/s: hibernate-core
    • Labels:
      None
    • Environment:
      OpenJDK 8, Glassfish 4.1.1
    • Last commented by a user?:
      true

      Description

      Hibernate performs useless class lookup when applying the constant conversion. For instance, in the query SELECT p FROM Event p WHERE p.eventstart<?1, there will be a class lookup for ch.astorm.entities.Event and for p.eventstart and this will occur each time the query needs to be compiled by Hibernate.
      There will be a warning in the logs just as mentioned in the issue HHH-11245 Closed .

      The "randomess" described in HHH-11243 Closed is caused by query plan caching (see HHH-4627 Closed ). Hence, to reproduce the warning systematically, a "new" query needs to be given to Hibernate, which is somewhat easy if they are not parametrized.
      So, I set the property hibernate.query.plan_cache_max_size to 40 (which will be the minimum accepted value because of the BoundedConcurrentHashMap fixed parameters created in QueryPlanCache) and created a simple loop in each query to force renewal of the cache:

      entityManager.createQuery("SELECT p FROM Event p WHERE p.eventstart<=?1 AND p.eventend>=?2", Event.class);
      for(int qp=0 ; qp<40 ; ++qp) {
              //generate a different query each time to force the query cache plan to renew
              entityManager.createQuery("SELECT p FROM Event p WHERE 0="+qp);
      }
      

      My patch suggests transforming the method handleDotStructure in QueryTranslatorImpl (line 613) to:

      private void handleDotStructure(AST dotStructureRoot) {
      	final String expression = ASTUtil.getPathText( dotStructureRoot ); 
      	final Object entity = factory.getMetamodel().entity( expression );
      	if( entity == null && expression.indexOf('.') != -1 ) { //avoid some useless lookups
      		//lookup & constant transformation, if any
      	}
      }
      

      This check will avoid entity lookups (because the expression ch.astorm.entities.Event will cause a lookup on ch.astorm.entities which will be always null), but the patterns like p.eventstart will still cause IMHO a useless lookup which could probably be avoided by using the parsed HQL query.

      Any suggestion is welcome.
      Thanks in advance.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: