Spring Framework
  1. Spring Framework
  2. SPR-6636

Missing package import for javax.persistence.criteria in ORM

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Complete
    • Affects Version/s: 3.0 GA
    • Fix Version/s: 3.0.3
    • Component/s: Data
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      In ORM, dynamic proxies for EntityManagers are created (in SharedEntityManagerCreator.createSharedEntityManager())

      When creating dynamic proxies, all classes that the to be proxified class depends on, must be on classpath.

      Since JPA 2.0.0, EntityManager has a dependency on javax.persistence.criteria.

      Thus, a (optional) package import on javax.persistence.criteria must be added to the MANIFEST.MF of ORM bundle.

      Otherwise, we get NoClassDefFoundError for class javax.persistence.criteria.CriteriaQuery, when using @PersistenceContext in OSGi environments.

      Note: There are some cases, when this error not occurs, for example, if the EntityManagerFactory created in the same bundle as where @PersistenceContext is used and if this bundle has some import on javax.persistence.criteria.
      But in other cases, when EntityManagerFactory is provided as an OSGi-service and used by other (DAO-)Bundles, we have no chance to workaround the missin import, since in those cases, always the Bundle classloader of ORM is used:

      108 if (emf instanceof EntityManagerFactoryInfo)

      { 109 cl = ((EntityManagerFactoryInfo) emf).getBeanClassLoader(); 110 }

      111 Class[] ifcs = new Class[entityManagerInterfaces.length + 1];
      112 System.arraycopy(entityManagerInterfaces, 0, ifcs, 0, entityManagerInterfaces.length);
      113 ifcs[entityManagerInterfaces.length] = EntityManagerProxy.class;
      114 return (EntityManager) Proxy.newProxyInstance(
      115 (cl != null ? cl : SharedEntityManagerCreator.class.getClassLoader()),
      116 ifcs, new SharedEntityManagerInvocationHandler(emf, properties));

        Activity

        Hide
        Stefan Ocke added a comment -

        Sorry for bad code formatting above. I should also mention, that this code comes from SharedEntityManagerCreator.createSharedEntityManager().

        Show
        Stefan Ocke added a comment - Sorry for bad code formatting above. I should also mention, that this code comes from SharedEntityManagerCreator.createSharedEntityManager().
        Hide
        Juergen Hoeller added a comment -

        Thanks for raising this! I've updated the manifest accordingly.

        Juergen

        Show
        Juergen Hoeller added a comment - Thanks for raising this! I've updated the manifest accordingly. Juergen
        Hide
        Michael Pilquist added a comment -

        After upgrading an OSGi application to Hibernate 3.5 + javax.persistence 2.0.0, I'm getting this same issue. I checked the manifest of org.springframework.orm – both 3.0.1.RELEASE-A and 3.0.2.RELEASE – and neither have the import-package for javax.persistence.criteria. Did this fix get removed accidentally?

        Without the fix, these types of errors occur:

        Caused by: java.lang.NoClassDefFoundError: javax.persistence.criteria.CriteriaQuery
        	at $Proxy84.<clinit>(Unknown Source)
        	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        	at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:588)
        	at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:118)
        	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:667)
        	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:629)
        	at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147)
        	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)
        	at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:338)
        	... 15 more
        
        Show
        Michael Pilquist added a comment - After upgrading an OSGi application to Hibernate 3.5 + javax.persistence 2.0.0, I'm getting this same issue. I checked the manifest of org.springframework.orm – both 3.0.1.RELEASE-A and 3.0.2.RELEASE – and neither have the import-package for javax.persistence.criteria. Did this fix get removed accidentally? Without the fix, these types of errors occur: Caused by: java.lang.NoClassDefFoundError: javax.persistence.criteria.CriteriaQuery at $Proxy84.<clinit>(Unknown Source) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:588) at org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(SharedEntityManagerCreator.java:118) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:667) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:629) at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:147) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:338) ... 15 more
        Hide
        Michael Pilquist added a comment -

        Looks like template.mf still looks correct. I wonder if the core build compiles against javax.persistence 1.0.0, which doesn't have the criteria package, and hence, bundlor is ignoring the javax.persistence.criteria.* import.

        Show
        Michael Pilquist added a comment - Looks like template.mf still looks correct. I wonder if the core build compiles against javax.persistence 1.0.0, which doesn't have the criteria package, and hence, bundlor is ignoring the javax.persistence.criteria.* import.
        Hide
        Juergen Hoeller added a comment -

        Indeed, the orm module gets compiled against the JPA 1.0 API jar which seems to make bundlor ignore that JPA 2.0 oriented Import-Template line completely... I've changed that import to an explicit Import-Package declaration for 3.0.3 which should hopefully solve this issue for good.

        Juergen

        Show
        Juergen Hoeller added a comment - Indeed, the orm module gets compiled against the JPA 1.0 API jar which seems to make bundlor ignore that JPA 2.0 oriented Import-Template line completely... I've changed that import to an explicit Import-Package declaration for 3.0.3 which should hopefully solve this issue for good. Juergen

          People

          • Assignee:
            Juergen Hoeller
            Reporter:
            Stefan Ocke
            Last updater:
            Trevor Marshall
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              4 years, 1 week, 6 days ago