About Paris Apostolopoulos

Paris is a senior software engineer focusing on J2EE development, loves Business process modelling and is keen on software quality challenges. He is passionate about Java and Java communities. He is a co-founder and administrator of the first Java User Group in greece(JHUG.gr) and occasional speaker on meet-ups and seminars and regular blogger.

Hibernate 4.2.8 , javassist 3.18.1 and ClassCastExceptions – watch out your classpath

I am writing this post as a tip and warning rather than an absolute solution. I will try to come back with a workaround for my case (WebSphere 8.5.5), but I am sure that other developers and applications  will be affected. I’ve lost some time trying to figure out the cause of the problem, so for the time being this post will help to increase the search index for the above keywords and link to the hibernate issue tracker.

In the current project I am working on, due to a bug in the Hibernate ORM version 4.2.0 library, I decided to move the library version forward on our setup and upgrade to the latest version. Hibernate currently features version 4.2.8 as the latest JPA 2.0 compliant version while 4.3.0 is the first official JPA 2.1 compliant. Since I am working on a Jee6 version, upgrading to 4.2.8 was my choice.

Eventually in 4.2.8 and onwards a ‘serious‘ internal API change was implemented within the  library. Hibernate moved to a new version of javaassist, that introduces some new internal API calls and interfaces. See linked issue here. So, version 4.2.8 will fetch as a dependency javassist- 3.18.1-GA to your app .

Eventually if you happen to introduce either

  • multiple dependencies to javassist versions
  • a container is providing an older version through a ‘parent’ classloader for your ear/war

Then you will end up with problems like this. In plain words if by chance an older version of javassist is picked up by the active classloader in your container, while loading jpa entities or executing related code you will be most probably end up with weird class cast exceptions or ‘class not found ‘ exceptions pointing to a deprecated javassist API.

IMHO, this change should have been introduced to a more ‘major’ version of Hibernate, but I trully understand that the development needs of the library sometimes can not be mandated by the different needs and setups of the worldwide developer user community. So this is just my opinion regarding the ‘should have been’, I still consider Hibernate the best Java ORM out there.

First actions in case you hit on this case:

  • Check your deployable, jar dependencies and find any other instances of the javassist especially older versions- check depending on your container and classloading settings how they are loaded.
  • In case you don’t include explicitly any extra dependencies and the only instance is the one ‘fetched from Hibernate’ (like my case), then you need to examine if an older version of javassist is loaded from a application or server level shared library or from a ‘shared’ classloader in your application server. (I am still working on this one, for Websphere 8.5.5). You can experiment with shared library loading order or classloading mode (parent first/parent last)
  • If you can’t find the solution based on the 2 above points or you tried to experiment with your class loading policy on your app and proved to be diasterous then, just switch to version 4.2.7 which seems to be the last one that is backwards compatible with older versions of javassist.

If you are a JBoss EAP 6.2 and JBoss Wildfly 8 user, you are on the safe side, meaning these containers already ship with the ‘latest’ javassist version, but you still need to sort out any potential conflicting versions your application might introduce.

Hopefully I will come back with a workaround for my case.
 

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


5 × four =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books