0 Replies Latest reply on Mar 9, 2006 7:34 PM by arjan tijms

    How are the persistence classes in root of unit located?

    arjan tijms Novice

      In section 6.2.16 of the java persistence spec (Final draft) I read the following:


      The set of managed persistence classes that are managed by a persistence unit is defined by using one or
      more of the following:
      ...

      The annotated managed persistence classes contained in the root of the persistence unit (unless
      the exclude-unlisted-classes element is specified)


      How does the current version of Hibernate handle this? I'm using Hibernate Core 3.1.1 with Annotations 3.1b8 and EntityManager 3.1b6 in combination with Tomcat 5.5.15 and Sun JDK 1.5u6 on Windows XPsp2.

      If I list the classes explicitly in my persistence.xml, everything works as expected and I can make use of a lot of EJB3 facilities. However, as soon as I remove the explicit class listing, everything Hibernate tries to lookup (e.g. named queries, entities, etc) fails. E.g. I get exceptions like: org.hibernate.MappingException: Named query not known: MyClass.MyQuery

      I didn't set the exclude-unlisted-classes element, but even if I would have done that, it doesn't seem to be used in the source code. PersistenceMetadata.java has a getExcludedUnlistedClasses method, but it's just never called from anywhere (or so it seems).

      I tried to work my way through the initialisation part of an entity manager, but I can't spot the code anywhere that scans the root of the persistence unit for classes that contain EJB3 annotations.

      Specifically, the parts that puzzle me are:

      In EJB3Configuration.createFactory:
      
       if ( metadata.getClasses().size() > 0 ) {
       workingVars.put( HibernatePersistence.CLASS_NAMES, metadata.getClasses() );
       }
      
      
      
      In Ejb3Configuration.createEntityManagerFactory:
      
       if ( workingVars.containsKey( HibernatePersistence.CLASS_NAMES ) ) {
       // check and add to cfg.annotatedClasses CLASS_NAMES ultimately come from persistence.xml
      
       }
      
       if ( workingVars.containsKey( HibernatePersistence.LOADED_CLASSES ) ) {
       // check and add to cfg.annotatedClasses. LOADED_CLASSES are never inserted anywhere (?)
       }
      
       // ...
      
       listenerConfigurator.configure() -> ... -> AnnotationConfiguration.secondPassCompile ->
       // process annotations in classes and fill cfg.classes Map.
      


      It seems though that HibernatePersistence.LOADED_CLASSES should be the key under which the classes from the root of the unit are stored, but this key is never inserted, nor could I locate any code that attempts this.

      Did I just overlook something? I know that other people don't list their classes at all in persistence.xml and they still get processed by Hibernate, so where could the code be that does this? Why would it possible not work in my situation?