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

Use javax.persistence.Query#unwrap to implement addNamedQuery, rather than direct type checking

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects versions: 4.3.0.Beta3
    • Fix versions: 4.3.0.Beta4
    • Components: None
    • Labels:
      None
    • Environment:
      Wildfly AS
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      Description

      An app-server might wrap queries for various reasons. The initial report here actual came about because WildFly App Server does in fact wrap the javax.persistence.Query instances Hibernate produces in non-transactional cases.

      Currently Hibernate does a direct type check to implement javax.persistence.EntityManagerFactory#addNamedQuery, checking that the incoming query is of type org.hibernate.jpa.HibernateQuery. This fails when WildFly has wrapped the query.

      Same for the javax.persistence.StoredProcedureQuery branch inside #addNamedQuery too.

      Initial report:


      Adding a named query at runtime:

      
         @PersistenceContext
         private EntityManager em;
      
         @PostConstruct
         public void initNamedQuery()
         {
            em.getEntityManagerFactory().addNamedQuery("customersOrderByName",
                     em.createQuery("from Customer c ORDER BY c.name DESC"));
         }

      Results in:

      Caused by: javax.persistence.PersistenceException: Cannot use query non-Hibernate EntityManager query as basis for named query
      	at org.hibernate.jpa.internal.EntityManagerFactoryImpl.addNamedQuery(EntityManagerFactoryImpl.java:367) [hibernate-entitymanager-4.3.0.Beta3.jar:4.3.0.Beta3]
      	at org.javaee7.samples.jta.TestServlet.initNamedQuery(TestServlet.java:33) [classes:]
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_21]
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_21]
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_21]
      	at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_21]
      	at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptorFactory$ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptorFactory.java:130)
      	at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      	at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
      	at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      	at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptorFactory.java:115)
      	at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      	at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
      	at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      	at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.java:28)
      	at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      	at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:55)
      	at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      	at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:325)
      	at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_21]
      	at org.jboss.invocation.PrivilegedInterceptor.processInvocation(PrivilegedInterceptor.java:75)
      	at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
      	at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
      	at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:160)
      	... 34 more
      

      Example app (commit): https://github.com/lincolnthree/con4456-coding-ee-7-easy-easier/commit/9749b9e94f51a3c3bbdca144ed5c5637cc854f1a

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - Not Specified
                Not Specified
                Logged:
                Time Spent - 1.95h
                1.95h