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(KeyFactory.java:165)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
at net.sf.cglib.proxy.Enhancer.(Enhancer.java:69)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.getProxyFactory(CGLIBLazyInitializer.java:107)
at org.hibernate.proxy.pojo.cglib.CGLIBProxyFactory.postInstantiate(CGLIBProxyFactory.java:43)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildProxyFactory(PojoEntityTuplizer.java:162)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.(AbstractEntityTuplizer.java:135)
at org.hibernate.tuple.entity.PojoEntityTuplizer.(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.(EntityEntityModeToTuplizerMapping.java:56)
at org.hibernate.tuple.entity.EntityMetamodel.(EntityMetamodel.java:295)
at org.hibernate.persister.entity.AbstractEntityPersister.(AbstractEntityPersister.java:434)
at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:109)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.(SessionFactoryImpl.java:226)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:805)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:745)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1175)

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.

2 comments:

PapaMinos said...

Please, could you show your pom file?
I've got same problem

Anonymous said...

Yes, thanks