-
1. Re: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast
ffang Apr 4, 2011 11:44 PM (in response to jcoguser)Hi,
As one bundle works but another one doesn't, and those two bundles actually doing same thing, I suggest you just use
osgi:headers
to see the different Import-Package of each bundle, this may inspire you.
Freeman
-
2. Re: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast
jcoguser Apr 6, 2011 3:32 PM (in response to ffang)Thanks Freeman for your reply. I followed your suggestion and did found some discrepancies in the imports. After moving a few things around, I got the imports for the 2 bundles to match. However, I still got the same error.
Then I traced a request through for each of the 2 database lookup bundles. I found out that the failing bundle has a select statement with an "IN (:list)" clause. Eventually the :list gets replaced by many instance variables (: variables) as there are in the actual list. This replacement causes the query to be parsed again during the query.getResultList(). Here is when the exception gets thrown. I narrowed it down to the antl.Utils.loadClass(String name). See code snippet below. Now my question: Is this another case of a class using the current thread's classloader to load a class that such a classloader has not visibility to, but the bundle's does.
In any case, how do I get around it?
Thanks in advance.
public static Class loadClass(String name) throws ClassNotFoundException {
try {
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (!useDirectClassLoading && contextClassLoader!=null ) {
return contextClassLoader.loadClass(name);
}
return Class.forName(name);
}
catch (Exception e) {
return Class.forName(name);
}
}
-
3. Re: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast
sorin7486 Apr 26, 2011 10:59 AM (in response to jcoguser)Hi,
have you found a solution to this ? I have a similar problem in ServiceMix 4.3.0.
-
4. Re: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast
sorin7486 Apr 26, 2011 11:12 AM (in response to sorin7486)Actually for me it seems that setting the following property did the trick:
-
5. Re: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast
jcoguser Apr 26, 2011 11:32 AM (in response to sorin7486)Sorin.
I found a workaround that consisted in overriding the thread classloader with the bundle classloader. If you need details let me know; however, it seems you found a solution.
-
6. Re: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast
sorin7486 Apr 28, 2011 6:28 AM (in response to jcoguser)Hi,
It's not a verry good solution because it throws a different error on certain queries and when I google it all the answers I get say I should be using antlrn! This thing is starting to get on my nerves.
How did you overwrite the thread classloader ? Does that have any other side effects?
-
7. Re: org.hibernate.QueryException: ClassNotFoundException: org.hibernate.hql.ast
sorin7486 Apr 28, 2011 8:54 AM (in response to jcoguser)There's another workaround on this page:
http://www.yarenty.com/tools/java/56-osgihibernatepart1.html?showall=1
I tried it but without any luck. I had it with Hibernate so I think I'll give openjpa a go.
-
8. Re: ClassNotFoundException: org.hibernate.hql.ast
jecalvert Apr 28, 2011 8:59 AM (in response to jcoguser)I would be very interested to see what you did. The question is where does the answer lie? It is dumb that the HQL lexer passes a class by name, but also dumb that ultimately there's some Util class using the Thread context class loader. Fuse ESB is already using "Apache ServiceMix :: Bundles :: antlr (2.7.7.3)" Would it be silly or reasonable to fork antlr and fix the "Utils" class?
An alternative is to note that HqlLexer has antlr.CharScanner as a superclass; the call in CharScanner gets the class and then sets it to a protected field. I can't imagine why setTokenObjectClass() in HqlLexer doesn't just bypass the super and set the class directly via
tokenObjectClass = HqlToken.class;
ESPECIALLY since it already throws away the string argument. At that point, one might even petition the Hibernate gods with a patch...
-
9. Re: ClassNotFoundException: org.hibernate.hql.ast
jcoguser Apr 28, 2011 10:43 AM (in response to jecalvert)This is what it was suggested to me. It solved my immediate problem but haven't done much testing to verify it didn't break anything else. As I said in a previous posting, the hack consisted in overriding the thread's class loader before the call to the method that will eventually result in the ClassNotFound exception. query.getResultList() in my case. The thread's original class loader is restored back after the "failing" call is completed. Good luck!
ClassLoader cl = null;
try {
Query query =
createNamedQuery ("MyEntityObject.myNamedQuery");
query.setParameter("param1", getParam1());
query.setParameter("param2", getParam2());
cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
List();
}
catch (Exception e) {
throw new MyDatabaseException(e);
}
finally {
if ( cl != null )
Thread.currentThread().setContextClassLoader(cl);
}
-
10. Re: ClassNotFoundException: org.hibernate.hql.ast
jecalvert Apr 28, 2011 11:28 AM (in response to jcoguser)I created an issue in the Hibernate JIRA; not sure if anyone will reply.
http://opensource.atlassian.com/projects/hibernate/browse/HHH-6165
I have applied my simple patch backported to the standard Fuse hibernate bundle and I confirmed that it fixed the ClassNotFound for me.
Really if you look at the code, there is no reason why the HqlLexer should be passing class by it's name to get loaded in another package when it can simply set it on the protected field directly, since it is a subclass. Of course, this is something that no one would really bother to notice unless you were in an OSGi env.
https://github.com/jcalvert/hibernate-osgi-fix
That's my github where I've dumped the patched source and put a JAR file for anyone who needs it. I would be delighted to know that anyone else who tries it if it also fixed their exceptions.
hoping someone from fusesource notices this
-
hqllexer.patch 509 bytes
-
-
11. Re: ClassNotFoundException: org.hibernate.hql.ast
davsclaus Apr 28, 2011 11:38 AM (in response to jecalvert)Unfortunately Hibernate is most likely the most OSGi unfriendly framework out there.
OpenJPA works much better in OSGi.
-
12. Re: ClassNotFoundException: org.hibernate.hql.ast
sorin7486 May 4, 2011 9:20 AM (in response to davsclaus)Yes I migrated to OpenJPA and everything works now. But if Hibernate is so unfriendly why do I get the feeling that everything there is more support for it than for OpenJPA?
-
13. Re: ClassNotFoundException: org.hibernate.hql.ast
davsclaus May 4, 2011 1:52 PM (in response to sorin7486)Hibernate was created much long before OpenJPA. In fact JPA spec is heavily based on Hibernate after it has proven itself after many years.
OpenJPA is used by IBM in their offerings as their JPA implementation. And IBM is going down the OSGi road. So rest assured OpenJPA will play nicely with OSGi.