Home > Hibernate, Java, WebLogic > ClassNotFoundException: org.hibernate.hql.ast.HqlToken

ClassNotFoundException: org.hibernate.hql.ast.HqlToken

Another exception under WebLogic server. This exception occurs because the default query factory class in persistence.xml is the org.hibernate.hql.ast.ASTQueryTranslatorFactory. As the name suggests this is a classpath issue. You might be wondering why you get it since you have the hibernate jar file (the file that contains the HqlToken class) in your classpath. The reason is that the weblogic.jar contains a version of antlr.jar which is loaded by a classloader, while the hibernate stuff are loaded by another classloader. So when the server runs it’s using the version of antlr bundled with weblogic which cannot see the hibernate classes. There are two solutions to this problem

1) Use a

<property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>

instead of a

<property name="hibernate.query.factory_class" value="org.hibernate.hql.ast.ASTQueryTranslatorFactory"/>

in your persistence.xml. This will solve the problem but you will have additional issues if you use Enums as parameters to queries (like I do) since I found out the the ClassicQueryTranslatorFactory has problems translating Enum data types to parameters (instead it translates them as bytes, unless you pass the actual value itself and not the Enum object).

2)

Add Hibernate’s antlr jar file as first entry in the classpath (in your startWeblogic script of your domain’s bin folder) and force weblogic to use this instead. I copied it into the common/lib folder and used

set CLASSPATH=%WL_HOME%\common\lib\antlr-2.7.6.jar;%SAVE_CLASSPATH%

There is another solution suggested, to add the following

    <prefer-application-packages>
        <package-name>antlr.*</package-name>
    </prefer-application-packages>

in your weblogic-application.xml, but for some reason this did not work for me.

Advertisements
  1. Raju Errabelli
    3 December 2010 at 10:25 am

    Exactly I faced problem by using weblogic as u explained above. I tried by ur explanation now update and delete queries are working in my application by using HQL.

    Thanks a lot.

  2. Sanjay.Bhatol
    2 March 2011 at 11:07 pm

    Other way is to set the antlr jar version which you are using in the project in MANIFEST.MF file in the project as ‘Class-Path’ (example: Class-Path: antlr-2.7.6.jar) which will force the application to use antlr-2.7.6.jar and the problem will be resolved.

    I tried this approach and works fine with applcation

  3. ram
    22 June 2011 at 12:35 pm

    thanks alots for providing the good guidence ,,now it working fine

  4. lkafle
    11 August 2011 at 5:17 am

    great article thanks

  5. Luiz
    25 August 2011 at 12:18 am

    Had to post a THANK YOU.

    Best thing to do IMO was to add this jar to the classpath inside startWeblogic script.

    Was looking all over the net but only YOU showed how to do it..

  6. 16 November 2011 at 6:54 pm

    cool

  7. NULLPOINTER
    23 February 2012 at 2:37 pm

    Thanks a lot man!

  8. 24 February 2012 at 10:30 am

    Pano many thanks – I just bumped into this with the following setup.
    -Weblogic 10.3.4
    -hiberante 3.2.GA
    -antlr -2.7.6

    Weblogic has antlr 2.7.7 – bundler and this screws everything.

    I had the correct entry on the Manifest (MF) as suggested above – but it did not work!So I modified the startScript.

    If you ask me this is something that Weblogic should handle better! 😦

  9. 24 February 2012 at 11:28 am

    Pari as you can see I had to modify the start up script too. Changing the weblogic-application.xml file for some reason does not work (although according to Oracle this is the preferred solution). If you ask me it’s probably an issue with the classloading.

  10. Erikas
    11 May 2012 at 11:17 am

    Thanks man, I wasted half day trying to solve this issue.

  11. Vic
    13 July 2012 at 1:52 pm

    For Weblogic 10.3.5 the solution was to add “set PRE_CLASSPATH=%WL_HOME%\common\lib\antlr-2.7.6.jar” in commEnv.cmd

  12. Pon
    18 December 2012 at 12:06 pm

    if I would want to use the following how can I use in Entity class as I do not have cfg.xml for mapping?

    org.hibernate.hql.classic.ClassicQueryTranslatorFactory

    Thanks

  13. 18 December 2012 at 2:18 pm

    You define it in the persistence.xml, not in your cfg file.

    • Pon
      18 December 2012 at 2:32 pm

      Thanks for the reply. If I have applicationContext.xml how could do I refer to persistence.xml? Any examples? Thanks

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: