title: Map Entities loaded dynamically from external jars or outside classpath
body: <p>I need to map Entities that are not listed at hibernate.cfg.xml, those classes are loaded dynamically 
  from an arbitraty folder. I'm trying to register a ClassLoaderService to change the loading behavior, the
  following code runs fine if the classes are defined at compile time and exist in the classpath, but if
  I try to map a dinamically loaded class I get ClassNotFoundException. There are some questions about the same issue, but I didn't find any working solution.</p>
  
  <pre><code>URL file = ConsultaBase.class.getProtectionDomain().getCodeSource().getLocation().toURI().resolve("implementacao/").resolve("hibernate.cfg.xml").toURL(); 
  Configuration configuration = new Configuration()
      .addAnnotatedClass(Registro.class).configure(file);         
  
  ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder (
      new BootstrapServiceRegistryImpl(
          new ClassLoaderServicePirilampo(Registro.class.getClassLoader()), 
          new LinkedHashSet&lt;Integrator&gt;()
      )
  )
  .applySettings(configuration.getProperties())
  .addService(ClassLoaderService.class, new ClassLoaderServicePirilampo())
  .build();
  
  //this line throws ClassNotFoundException
  sessionFactory = configuration.buildSessionFactory(serviceRegistry);    
  </code></pre>
  
  <p>I extended the ClassLoaderServiceImpl in order to log the requested classes, and noticed that running from JUnit, from the project where the classes are defined, it works fine, I get the class loading log from the Service. But the Service never receives
  the request for the same class if I addAnnotatedClass that was loaded dinamically (from GroovyClassLoader).</p>
  
  <p>The last line throws de folowing error:</p>
  
  <pre><code>17:06:49 ERROR [AssertionFailure] HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.ClassNotFoundException: implementacao.Registro
  PersistentClass name cannot be converted into a Class
  org.hibernate.AssertionFailure: PersistentClass name cannot be converted into a Class
      at org.hibernate.cfg.BinderHelper.getPropertyOverriddenByMapperOrMapsId(BinderHelper.java:817)
      at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2169)
      at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
      at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
      at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
      at implementacao.ConsultaBase.createSessionFactory(ConsultaBase.java:64)
      at implementacao.ConsultaBase.consultar(ConsultaBase.java:92)
      at implementacao.ConsultaBase$consultar.call(Unknown Source)
      at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
      at ConsultaBaseConector.run(ConsultaBaseConector.groovy:6)
      at groovy.util.GroovyScriptEngine.run(GroovyScriptEngine.java:551)
      at br.org.fplf.processo.maquinaexecucao.parser.ParserAtividadeAutomatica.executar(ParserAtividadeAutomatica.java:43)
      at br.org.fplf.processo.maquinaexecucao.MaquinaExecucao.executarAtividadeAutomatica(MaquinaExecucao.java:1050)
      at br.org.fplf.processo.maquinaexecucao.MaquinaExecucao.executarAtividadeFluxo(MaquinaExecucao.java:973)
      at br.org.fplf.processo.maquinaexecucao.MaquinaExecucao.executar(MaquinaExecucao.java:646)
      at br.org.fplf.processo.maquinaexecucao.MaquinaExecucao.run(MaquinaExecucao.java:368)
  Caused by: java.lang.ClassNotFoundException: implementacao.Registro
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Unknown Source)
      at org.hibernate.annotations.common.util.ReflectHelper.classForName(ReflectHelper.java:60)
      at org.hibernate.annotations.common.reflection.java.JavaReflectionManager.classForName(JavaReflectionManager.java:138)
      at org.hibernate.cfg.BinderHelper.getPropertyOverriddenByMapperOrMapsId(BinderHelper.java:813)
      ... 20 more
  </code></pre>