@brmeyer I hadn't seen that those four properties were deprecated, "hibernate.classLoaders" is definitely the right choice in that case.

I also agree with updating Ejb3Configuration#buildEntityManagerFactory. The only reason I made the changes inline was to allow a Hibernate expert (i.e. you) to decide where the best place to put the fix was.

Having looked more closely at the Ejb3Configuration source I can see what you mean about the overriddenClassLoader. Using the TCCL isn't very OSGi-friendly, but it's obviously not sensible to try removing them all in one go! Is there any way to set the overridden classloader on a per-persistence unit basis? The big issue with trying to use a single overridden classloader is that it has to "flatten" the framework class space. Doing this can cause all manner of weird problems (some of which were already identified in comments in the OSGiClassLoader).

Ideally  we would be able to set the overridden classloader for the persistence unit to be a single classloader with the following search order:
1. Hibernate core bundle
2. Hibernate entitymanager bundle
3. The Persistence Unit ClassLoader

This will give the "best" unified view of the classes hibernate needs to access, and obviously requires a different overridden ClassLoader per persistence unit. Searches 1 and 2 are already done by the HibernateOSGiClassLoader.

>Or, is the point purely that the PersistenceUnitInfo#getClassLoader needs added to the providedClassloaders?

Unfortunately this isn't *quite* enough. The Hibernate entitymanager bundle provides some classes that need to be loaded by Hibernate core when configuring the SessionFactory (e.g. org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider). Quite rightly Hibernate core does not directly depend on Hibernate entitymanager, however this means that the ClassLoaderService for the persistence unit needs to be able to load from the Hibernate entitymanager bundle as well as the Hibernate core bundle and the persistence unit. This is why I modified the OSGiClassLoader to be a "HibernateOSGiClassLoader" that unified the two Hibernate bundles.