pre configured netbeans project with NoSuchMethodError hibernate and spring conflict

It looks like you're missing a library that Hibernate needs at run time. I saw the same behavior as you with your code (MySQL 5, MySQL JDBC driver, Mac OS) until I changed the line:

catch (Exception e) {
      System.out.println(e.getMessage());
}

To:

catch (Throwable e) {
       e.printStackTrace();
}

I then started seeing a whole set of NoClassDefFoundError and ClassNotFoundError messages about libraries that Hibernate was looking for but were not included in my CLASSPATH. I suspect that you're missing a library that Hibernate needs, and because you're catching Exception and not Throwable - which catches Error - you're not seeing the error message. See:

Why catch Exceptions in Java, when you can catch Throwables?

If you catch the Throwable you'll see fairly quickly what libraries and classes you're missing: my guess would be that you're probably missing EHCache (which Hibernate seems to use as a second-level cache by default), CGLIB/ASM, or the Java Transaction API. If you're missing EHCache and you want hibernate to use its own, in-memory Hashtable cache instead of EHCache, add the line below to hibernate.cfg.xml:

<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>

Update, based on comments to answer:

I don't use NetBeans and so I haven't run into this problem, but it appears fairly widespread. See:

Error : java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V

http://www.hildeberto.com/2008/05/hibernate-and-jersey-conflict-on.html (Reports a similar problem with Hibernate and Jersey in Netbeans)

https://hibernate.onjira.com/browse/HHH-2222 (Hibernate bug report mentioning this problem)

http://netbeans.org/bugzilla/show_bug.cgi?id=145589 (Netbeans bug report for bundled Hibernate using older versions of cglib).

The StackOverflow post linked to above has quite a lot of detail. To summarize:

  • Hibernate 3.2 uses CGLib 2.1.3 for run-time code generation, to improve performance and to generate proxies for one-to-one and one-to-many mappings.

  • CGLib is a higher-level wrapper around ASM, a bytecode manipulation library. CGLib 2.1.3 requires ASM 1.5.3, which is binary incompatible with ASM 2.2. ASM 2.2 in turn is a dependency for Spring versions < 2.5.

  • To resolve these problems between Hibernate and Spring, Spring 2.5 bundles its own version of asm with a spring package name; later versions of Hibernate use CGLIB 2.2, which also bundles its own version of ASM with a custom package name. The most recent versions of Hibernate do away with CGLIB altogether and use Javassist instead, but NetBeans still bundles Hibernate 3.2.5.

You have a few options, then:

  • Update the Hibernate library package in Netbeans with CGLIB 2.2

  • Tell Hibernate to use Javassist for runtime code generation. Add this line to hibernate.properties, or specify it as a system property using -D (you can't specify this property in hibernate.cfg.xml, apparently):

    hibernate.bytecode.provider=javassist

Good luck, this has been rather an interesting question!