-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Loading classes from outside classpath dynamically
PostPosted: Thu Dec 04, 2014 6:00 pm 
Newbie

Joined: Thu Dec 04, 2014 5:58 pm
Posts: 2
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 de 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.

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<Integrator>()
         )
      )
          .applySettings(configuration.getProperties())
          .addService(ClassLoaderService.class, new ClassLoaderServicePirilampo())
          .build();
               
      //this line throws ClassNotFoundException
       sessionFactory = configuration.buildSessionFactory(serviceRegistry);


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, I get the class loading log from the Service, but the Service never receive
the request for the same class if I addAnnotatedClass that was loaded dinamically (from GroovyClassLoader).

The last line throws de folowing error:

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


Last edited by gustavobapp on Fri Dec 05, 2014 3:14 pm, edited 5 times in total.

Top
 Profile  
 
 Post subject: Re: Loading classes from outside classpath dynamically
PostPosted: Fri Dec 05, 2014 3:11 pm 
Newbie

Joined: Thu Dec 04, 2014 5:58 pm
Posts: 2
This worked for me:
Code:
Thread.currentThread().setContextClassLoader(DynamicallyLoadedClass.getClassLoader()); 

example:
Code:
URL file = ConsultaBase.class.getProtectionDomain().getCodeSource().getLocation()
    .toURI().resolve("hibernate.cfg.xml").toURL();

Configuration configuration = new Configuration()
    .addAnnotatedClass(Registro.class).configure(file);         

ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
    .applySettings(configuration.getProperties())
    .build();

Thread.currentThread().setContextClassLoader(Registro.class.getClassLoader());

sessionFactory = configuration.buildSessionFactory(serviceRegistry);


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.