ClassCastException when using a single region name for both entity and query results in Hibernate

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7.2
  • Hibernate 5.3
  • Infinispan 9.3

Issue

  • Declared a named cacheable query for an entity and specifying the same region name for both the query and the entity.

    @NamedQuery(name = "Employee.findAll", query = "SELECT a FROM Employee a",
            hints = {
                    @QueryHint(name = "org.hibernate.cacheable", value = "true"),
                    @QueryHint(name = "org.hibernate.cacheRegion", value = "custom-region-01")
           }
    )
    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region="custom-region-01")
    
  • When executing the named query the first time, the exception below is raised:

    ... java.lang.ClassCastException: org.infinispan.hibernate.cache.v53.impl.DomainDataRegionImpl cannot be cast to org.hibernate.cache.spi.QueryResultsRegion
        at org.hibernate.cache.internal.EnabledCaching.makeQueryResultsRegionAccess(EnabledCaching.java:491)
        at org.hibernate.cache.internal.EnabledCaching.getQueryResultsCache(EnabledCaching.java:478)
        at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2515)
        at org.hibernate.loader.Loader.list(Loader.java:2498)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:504)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:395)
        at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:220)
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1508)
        at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1537)
        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505)
        at org.hibernate.query.Query.getResultList(Query.java:132)
        at org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.getResultList(QueryNonTxInvocationDetacher.java:58)
    

Resolution

This issue1 is resolved in This content is not included.EAP 7.2 cumulative patch (CP) 5 and later releases. Production systems should use the latest available cumulative patch.

Root Cause

This is a known defect (Content from hibernate.atlassian.net is not included.HHH-13586).

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.