Hibernate and Jersey Conflict on Glassfish

Last weekend I tried to use Jersey in an enterprise application to make some RESTful web services available. The idea was to generate data to be plot by flash charts. So, I created a new web module in addition to the already existent one. I thought it was better to keep it in a separate module to avoid changing the original web application, since it still uses Struts as web framework. Before adding any RESTful web service, I just created the web application and deployed it successfully as an additional enterprise module. I’m happy about that because I will use the same strategy to migrate the web module from Struts to JSF, keeping the Struts version working in parallel to the JSF version, both sharing the same business logic.

I use Netbeans because it makes my life easier. The Jersey module is available in the 6.1 version and for download in the update center for previous versions. Since installed, you may click with the right button on a web project to see the context menu and select “New … / RESTful Web Services from Patterns” or “New … / RESTful Web Services from Entity classes”. Since I’m not implementing entity classes in this module, I selected the one from patterns. It will start a wizard to prepare the application for REST and generate the initial class skeleton to start working.

I followed those steps and the RESTful web service was created successfully. However, when I start to deploy the whole application again, an obscure error appears in the Glassfish output log insistently. In such situation I tried everything: clean, build, clean and build, remove the module and deploy it separately, but nothing was enough to solve the problem below:

LifecycleException: java.lang.NoSuchMethodError: org.objectweb.asm.ClassReader.accept(Lorg/objectweb/asm/ClassVisitor;I)
Caused by: java.lang.IllegalStateException:
start: LifecycleException:
java.lang.NoSuchMethodError: org.objectweb.asm.ClassReader.accept(Lorg/objectweb/asm/ClassVisitor;I)

I checked if I was using the libraries appropriately, if they were correctly wrapped and if there was any library conflict. Yes, there is a library conflict with Hibernate, the persistence framework I am using as a JPA implementation. Jersey uses the ASM library, which is also used by Hibernate, but on different versions. Hibernate uses version 1.5.3 and Jersey uses 3.5.1. A big difference! We can not just delete the old version and put the new one there because they are architecturally different. Version 1.5.3 has an “attrs” package while 3.5.1 doesn’t have it but a “signatures” package. Changing the version can cause other problems and I’m not expecting them afterwards. So, how do I solve it consistently?

The ASM package is needed by the cglib package, which is part of the Hibernate libraries. If we remove that package, Jersey will work correctly, but Hibernate will stop working. To solve this conflict use cglib-nodep.jar instead of cglib.jar and keep ASM version 3.x with Jersey. cglib-nodep.jar includes some ASM classes demanded by cglib.jar, changing the package name to avoid any class conflict.

The ASM library is a Java bytecode manipulation and analysis framework. According to its website “(…) it can be used to modify existing classes or dynamically generate classes, directly in binary form. Provides common transformation and analysis algorithms to easily assemble custom complex transformations and code analysis tools“. ASM is used by many products like AspectJ, Oracle TopLink, JRuby, and many others. It can not be simply ignored by frameworks because it is a matter of flexibility. The best alternative is always to investigate the unexpected problem and claim for a better error presentation on the JVM.

45 thoughts on “Hibernate and Jersey Conflict on Glassfish

  1. Thanks for this post, really helped me. For no reason I can think of, Netbeans6.1 decided to add jersey to my web app, causing all the errors you stated. I just did an upgrade so maybe that triggered it, who knows, either ways, big thanks !!

  2. You should try using TopLink for your JPA implementation. Even though TopLink uses ASM, it won’t conflict with other layers that use different versions of it.

  3. Hi Mike! Actually, I started using Toplink Essentials at the beginning, but when errors occur they are not so clear like the ones generated by Hibernate. I moved to Hibernate also because Toplink is not in the long term plans of the Glassfish team, since they are adopting Eclipse Link instead of Toplink on Glassfish V3.

    Toplink Essentials is, actually, not so good like the one sold by Oracle. I think it was just a marketing strategy from Oracle.

  4. Hello –

    I too faced the same issue when using hibernate/jersey/jboss. What I did was download asm-3.0.jar file and put it in the jboss lib folder. and then restart jboss. and it solved the issue !

  5. Hello!
    very good solution. But there is a problem that any jsp or html page is not dispaying in the application where i had replaced the cglib and asm jars. The error http 404 is encountered when i tried to load any jsp or html. When I replaced back the orignal versions of cglib and asm, the html and jsp are visible correctly but jersey stops working. I am using tomcat 6.x server

  6. Hello Anonymous,

    Usually when this error happens you probably had a problem to deploy your application. HTTP 404 means nothing available on the server for the requested URL. So, your problem could be well described in the log files of Tomcat. Only with this logs is possible to figure out what is going on there.

  7. Awesome!
    I ran into the same problem with NetBeans 6.5, where I was using Hibernate JPA as well as Jersey.
    I just replaced cglib-2.1.3.jar [in the Hibernate distribution bundled with NetBeans] with cglib-nodep-2.2.jar], and it got rid of all my asm run-time exceptions.

    Thanks for the solution!

  8. Fantastic! Worked with the app I had created with NetBeans 6.7.1. I removed the asm, asm-attr and cgilib jars from the Hibernate JTA group of libraries, and manually added cgilib-nodep which I downloaded from sourceforge. Everything is now working on Tomcat 6. Thanks for posting this.

  9. while using the cglib-nodep-2.2.jar and JpaDaoSupport in spring , I am getting a new exception as java.lang.ClassCastException: $Proxy29 cannot be cast to. But this jar works fine for jpaTemplate.

  10. Thanks for the detailed information on the conflict and a solution to resolve it. Your solution worked like a charm. I hope one day Jersey and Hibernate can play nice but for now this works.

  11. Thank you for a Belgian for your help ;).
    I use Netbeans 7.0. I have removed the file /hibernate/cglib-2.1.3.jar. I have found /cglib-2.2 that I’have renamed by /cglib-nodep-2.2.jar and have added.


  12. Thank you! it solved my problem, was struggling with Hibernate and Jersey.
    I replaced cglib-2.1.3.jar by cglib-nodep-2.1_3.jar

    1. Hi I am facing the same problem when I use jersey + hibernate using eclipse..I have followed the above approach.. But doesnt work for me

  13. Hi Thanks for the article, It actually solves the issue when I was replaced the cglib-2.1.3 jar file with cglib-nodep-2.2 jar

Leave a Reply

Your email address will not be published. Required fields are marked *