Saturday, May 12, 2007

darn hibernate pom.xml

I just finished figuring out a problem for which I could not find any explanation online. It all started happening when I decided to take spring2 aop for a whirl. The app I am working on is a web app using spring and hibernate; both of them to the latest versions which 3.2.x for hibernate and 2.0.4 for spring.
My build system is maven based. The things I love about maven (for regular developer tasks)is its dependency resolution capablities as well as the eclipse:eclipse plugin which helps me setup eclipse in no time.
When I added a dependency on spring-aop and spring-aspects my application stopped deplying complainig about a class in asm that was not found. The exception was comming from hibernate trying to proxy an object using cglib. This was the stacktrace I was getting:
java.lang.NoClassDefFoundError: org/objectweb/asm/CodeVisitor
at net.sf.cglib.core.KeyFactory$Generator.generateClass(
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(
at net.sf.cglib.core.AbstractClassGenerator.create(
at net.sf.cglib.core.KeyFactory$Generator.create(
at net.sf.cglib.core.KeyFactory.create(
at net.sf.cglib.core.KeyFactory.create(
at net.sf.cglib.core.KeyFactory.create(
at net.sf.cglib.proxy.Enhancer.(
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(
at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(
at org.hibernate.tuple.entity.AbstractEntityTuplizer.(
at org.hibernate.tuple.entity.PojoEntityTuplizer.(
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(
at org.hibernate.tuple.entity.EntityMetamodel.(
at org.hibernate.persister.entity.AbstractEntityPersister.(
at org.hibernate.persister.entity.SingleTableEntityPersister.(
at org.hibernate.persister.PersisterFactory.createClassPersister(
at org.hibernate.impl.SessionFactoryImpl.(
at org.hibernate.cfg.Configuration.buildSessionFactory(
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(

and more stuff which adds no value to this blog entry.

The problem, after hours of digging, banging, pulling hair and snapping was the hibernate's pom.xml has cglig-2.1_3.jar as dependency and somehow this guy depends on asm-1.5.x.jar. Spring aop depends indirectly of asm-2.2.3 and maven, smart enough, was using the latter is its a newer version. To make a long story short, I added an exclussion to hibernate dependency for cglib and added cglib-nodep as one of my dependencies and this solved the problem.
I hope, if anyone hits this problem ever, he/she will google the stacktrace and will find this post.