Hibernate-Common Errors

Posted: April 13, 2012 in Hibernate

Hibernate – Common Errors:

1) Hibernate – Unable to insert if column named is keyword, such as DESC

Problem

A table named “category” in MySQL database, contains a “DESC” keyword as column name.

In database table column like     `DESC` VARCHAR(255) NOT NULL, 
In hbm.xml file   
 <property name="desc" type="string">
 <column name="DESC" not-null="true" />
 </property> 

Or hibernate Annotation: 
 @Column(name = "DESC", nullable = false)
 public String getDesc() {
 return this.desc;
 }

When insert into category table, hits following error message :

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:

Solution

In Hibernate, to insert into “keyword” column name, you should enclose it like this ‘[column name]‘.

In hbm.xml file

1. Enclose the keyword with square brackets [].

        <property name="desc" type="string" >
         <column name="[DESC]" length="255" not-null="true" />
        </property>

2. Use single quote(‘) to enclose the double quotes (“)

        <property name="desc" type="string" >
            <column name='"DESC"' length="255" not-null="true" />
        </property>

Or Hibernate annotation

1. Enclose the keyword with square brackets [].

        @Column(name = "[DESC]", nullable = false)
                    public String getDesc() {
                     return this.desc;
                    }

2. Use double quotes (“) to enclose it.

        @Column(name = "\"DESC\"", nullable = false)
                    public String getDesc() {
                    return this.desc;
                    }

Hibernate - Could not find C3P0 Connection Provider
Problem

Configured Hibernate to use “c3p0” connection pool, but hits following warning Look like “org.hibernate.connection.C3P0ConnectionProvider” is missing?

Solution

Since Hibernate v3.3 (if not mistaken), the “C3P0ConnectionProvider” is moved to another jar file “hibernate-c3p0.jar“. You need to include it, in order to make Hibernate supports c3p0 connection pool.

You can download the “hibernate-c3p0.jar” from JBoss public repository.

2) Hibernate – The type AnnotationConfiguration is deprecated

Problem

Working with Hibernate 3.6, noticed the previous “org.hibernate.cfg.AnnotationConfiguration“, is marked as “deprecated“.

Code snippets …

import org.hibernate.cfg.AnnotationConfiguration;
//...
private static SessionFactory buildSessionFactory() {
   try {
  return new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (Throwable ex) {
  System.err.println("Initial SessionFactory creation failed." + ex);
   throw new ExceptionInInitializerError(ex);
                    }
}

The code is still working, just keep displaying the deprecated warning message, is there any replacement for “AnnotationConfiguration” ?

Solution

In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration“.

So , you can safely replace your “AnnotationConfiguration” with “Configuration” class.

Code snippets …

import org.hibernate.cfg.Configuration;
//...
private static SessionFactory buildSessionFactory() {
  try {
  return new Configuration().configure().buildSessionFactory();
    } catch (Throwable ex) {
 System.err.println("Initial SessionFactory creation failed." + ex);
  throw new ExceptionInInitializerError(ex);
                    }
}
3) java.lang.ClassNotFoundException : javassist.util.proxy.MethodFilter
Problem

Using Hibernate 3.6.3, but hits this javassist not found error, see below for error stacks :

Solution

javassist.jar is missing, and you can get the latest from JBoss Maven repositorty.

4) org.hibernate.AnnotationException: Unknown Id.generator

Problem

Runing the following Hibernate’s annotation sequence generator with PostgreSQL database.

@Id              
@Column(name="user_id", nullable=false)            
@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="account_user_id_seq")
private Integer userId;

Hits the following Unknown Id.generator exception.

Caused by: org.hibernate.AnnotationException: Unknown Id.generator: account_user_id_seq
    at org.hibernate.cfg.BinderHelper.makeIdGenerator(BinderHelper.java:413)
    at org.hibernate.cfg.AnnotationBinder.bindId(AnnotationBinder.java:1795)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1229)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:733)

The sequence “account_user_id_seq” is created in PostgreSQL database, what caused the above exception?

Solution

When declaring the Hibernate’s annotation strategy to use “Sequences” as Id generator, try specify the @SequenceGenerator as well, as following

@Id      
@Column(name="user_id", nullable=false)            
@SequenceGenerator(name="my_seq", sequenceName="account_user_id_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE ,generator="my_seq")
private Integer userId;
5)Hibernate Error – Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/dom4j/DocumentException
A common Hibernate’s error, this is caused by the missing dependency library – dom4j.

Solution

You can download the library here – http://sourceforge.net/projects/dom4j/files/dom4j/

6)Hibernate Error – Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

A common Hibernate’s error, this is caused by the missing dependency library – commons-logging.jar

Solution

You can download the library here – http://commons.apache.org/logging/

7)Hibernate Error – Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap

A common Hibernate’s error, this is caused by the missing dependency library – commons-collections.jar

Solution

You can download the library here – http://commons.apache.org/collections/

8)Hibernate Error – Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter

A common Hibernate’s error, this is caused by the missing dependency library – cglib.

Solution

You can download the library here – http://cglib.sourceforge.net/

9)Hibernate Error – Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/DataSources

Hibernate’s “C3P0″ connection pool error, this is caused by the missing dependency library – C3P0.

Solution

You can download the library here – http://www.mchange.com/projects/c3p0/

10)Hibernate Error – Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/ReflectionManager

This is caused by missing of the Hibernate commons annotations library.

Solution

You can download the library from Hibernate official website

11)Hibernate Error – Exception in thread “main” java.lang.NoClassDefFoundError: antlr/ANTLRException

This is caused by missing of the antlr library. It’s usually happened when you did invoke Hibernate’s query statement.

Solution

You can download the library from Antlr official website

12)Hibernate Error – java.lang.NoClassDefFoundError: javax/transaction/Synchronization

Problem

This is caused by missing of the “jta.jar“, usually happened in Hibernate transaction development.

Solution

You can download “jta.jar” from default Maven central, JBoss or Java.net repositories.

13)java.lang.ClassFormatError : Absent Code attribute in method that is not native or abstract in class file …

Problem

A very strange and rare problem, happened in JPA or Hibernate development.

Solution

This is always caused by the javaee.jar which is located at Java.net. Many developers like to grab the javaee.jarwith the following Maven coordinate :

14)java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter

Problem

In Hibernate development, it’s common to hits the following error message.

Solution

The “Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer” is a generic error message, it may caused by many reasons. So, you have to look at the last line that caused the error.

Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter

The main cause is the old asm.jar library e.g ‘asm-1.5.3.jar’, just upgrade the asm library to the most recent version will get rid of the error message. e.g, ‘asm-3.1.jar’.

15) java.lang.ClassNotFoundException: javax.persistence.Entity

Problem

In JPA or Hibernate development, it hits the following error message :

Solution

The javax.persistence.Entity is a class inside the J2EE SDK library “javaee.jar“, you are missing this jar file in your project classpath.

1. J2EE SDK

You can always get the javaee.jar from http://java.sun.com/javaee/. Download and install the SDK in your computer, the javaee.jar can be found in the “\J2EE_SDK_FOLDER\lib” folder. For example,

C:\Sun\SDK\lib\javaee.jar

Get the javaee.jar file and include it in your project classpath.

16) java.lang.ClassNotFoundException: javax.transaction.TransactionManager

Problem

In JPA or Hibernate development, it hits the following error message :

Solution

The javax.transaction.TransactionManager is a class inside the J2EE SDK library “javaee.jar“, you are missing this jar file in your project classpath.

1. J2EE SDK

You can always get the javaee.jar from http://java.sun.com/javaee/. Download and install the SDK in your computer, the javaee.jar can be found in the “\J2EE_SDK_FOLDER\lib” folder. For example,

C:\Sun\SDK\lib\javaee.jar

Get the javaee.jar file and include it in your project classpath.

17)java.lang.ClassFormatError : Absent Code attribute in method that is not native or abstract in class file …

Problem

A very strange and rare problem, happened in JPA or Hibernate development.

Solution

This is always caused by the javaee.jar which is located at Java.net. Many developers like to grab the javaee.jar

Advertisements
Comments
  1. Ganapathy says:

    Hi Gopal …I saw ur blogs which is great..keep up the good work….-Ganapathy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s