Preconfigured netbeans project with hibernate NoSuchMethodError and spring conflict

so I try to connect my Java application to SQL Server 2012 through Microsoft JDBC Driver 4.0 for SQL Server , and it seems that everything is going fine, but hibernate just returns with NullExceptions and does not do anything in try/catch (hence NullException ), I absolutely have no idea why. Here is the pastebin from the netbeans console ( e.getMessage() ) on which hibernation mode is running (for the purpose of this question, I am using an example table called prime_table ).

In the insert log you will notice ...

Feb 11, 2013 5:21:04 PM org.hibernate.cfg.Configuration doConfigure INFO: Configured SessionFactory: null

any ideas on why this is happening? (I'm not sure, but this may have to do with the overall stack trace).

Other logs (during JSP build)

All magazines will be available until mid / end of March 2013

Resources I read

commandline: tree /f NetBeans project configuration commandline: tree /f

netbeans
(source: iforce.co.nz )

Hibernate.cfg.xml (added "hibernate.cache.provider_class" as proposed by Hari )

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property> <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> <property name="hibernate.connection.url">jdbc:sqlserver://Michael-PC:1433;databaseName=PrimeDB</property> <property name="hibernate.connection.username">sa</property> <property name="hibernate.connection.password">online12</property> <property name="hibernate.connection.pool_size">10</property> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Data Mappings --> <mapping resource="prime.hbm.xml"/> </session-factory> </hibernate-configuration> 

Prime

 public class Prime { private long prime; public void setPrime(long nPrime){ this.prime = nPrime; } public long getPrime(){ return this.prime; } } 

prime.hbm.xml

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="nz.co.xyleap.db.Prime" table="prime_table"> <id name="prime" type="long" column="prime" /> </class> </hibernate-mapping> 

Main table

 CREATE TABLE prime_table ( [prime] bigint PRIMARY KEY NOT NULL, ) 

Session Function or FirstExample.java

 public class FirstExample { public static void main(String[] args) { FirstExample ex = new FirstExample(); ex.session(); } public void session() { Session session = null; try { // This step will read hibernate.cfg.xml and prepare hibernate for use SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session = sessionFactory.openSession(); session.beginTransaction(); //Create new instance of Contact and set values in it by reading them from form object System.out.println("inserting record 13"); Prime p = new Prime(); p.setPrime(13); session.save(p); System.out.println("Done"); } catch (Exception e) { System.out.println(e.getMessage()); } finally { // Actual contact insertion will happen at this step session.flush(); session.close(); } } } 

As far as I know, everything should be correct, and I should be able to insert records from Java Hibernate into my SQL database. But for some reason I can not: - /

UPDATE: Hibernate itself, works fine. But in combination with Spring, these errors occur. (More in the comments).

+7
source share
2 answers

It looks like you are missing the library that Hibernate needs at runtime. 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(); } 

Then I started to see a whole set of messages NoClassDefFoundError and ClassNotFoundError about libraries that Hibernate was looking for, but was not included in my CLASSPATH. I suspect you are missing a library that needs Hibernate, and because you are catching an Exception , not Throwable - which is catching Error - you are not seeing an error message. Cm:

Why catch Exceptions in Java when you can catch Throwables?

If you catch Throwable , you will see pretty quickly which libraries and classes you are missing: I assume that you are probably missing EHCache (which Hibernate is apparently used as the default second level cache), CGLIB / ASM or Java Transaction API If you are missing EHCache and want hibernation to use its own Hashtable cache in memory instead of EHCache, add the line below to hibernate.cfg.xml :

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

Comment based update for reply:

I do not use NetBeans, and therefore I have not encountered this problem, but it is quite common. Cm:

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

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

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

http://netbeans.org/bugzilla/show_bug.cgi?id=145589 (Netbeans error report for linked sleep mode using older versions of cglib).

The StackOverflow post related to above has quite a few details. Summarizing:

  • Hibernate 3.2 uses CGLib 2.1.3 to generate code at runtime, to improve performance, and to create proxies for one-to-one and one-to-many comparisons.

  • CGLib is a higher-level shell 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 versions of Spring 2.5.

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

You have several options, and then:

  • Update Hibernate Library Package on Netbeans with CGLIB 2.2

  • Tell Hibernate to use Javassist to generate runtime code. Add this line to hibernate.properties or specify it as a system property using -D (you cannot specify this property in the hibernate.cfg.xml file, apparently):

    hibernate.bytecode.provider=javassist

Good luck, that was a pretty interesting question!

+5
source

He dies on session.flush (); because the session is null and something in the try block throws an exception, maybe it is a runtimeexception like nullpointer or something like that. The fact that the session is null will call another nullptr and it will hide the original one. So I suggest you do it

 if(session!=null) { session.flush(); session.close(); } 

and then run it to see what the actual problem is.

I also suggest replacing "System.out.println (e.getMessage ()); with the smallest value e.printStackTrace (); or preferably proper logging.

-one
source

All Articles