Hi! I am new in hibernate annotations so i am practising it on the console. In here i am using hibernate3.jar(came with Hibernate 3.5.1), hibernate-annotations-3.4.0.GA.jar etc.
However when i run the main the application didn't start and following errors were generated.
Any idea,please help.. !??

0 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.1-Final
31 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.1-Final
47 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
63 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
63 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
391 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
485 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
485 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
985 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: sessionFactory
1172 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.vaannila.course.Course
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: javax/persistence/Cacheable
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.vaannila.util.HibernateUtil.<clinit>(HibernateUtil.java:14)
at com.vaannila.course.Main.saveCourse(Main.java:27)
at com.vaannila.course.Main.main(Main.java:16)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/Cacheable
at org.hibernate.cfg.AnnotationBinder.determineCacheSettings(AnnotationBinder.java:946)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:589)
at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:359)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
at com.vaannila.util.HibernateUtil.<clinit>(HibernateUtil.java:11)
... 2 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Cacheable
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 9 more
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------

hibernate.cfg.xml :

<?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 name="sessionFactory">
        <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
        <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost</property>
        <property name="hibernate.connection.username">sa</property>
        <property name="connection.password"></property>
        <property name="connection.pool_size">1</property>        
        <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <mapping class="com.vaannila.course.Course" />        
    </session-factory>
</hibernate-configuration>

HibernateUtil.java :

package com.vaannila.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
	private static final SessionFactory sessionFactory;
	static {
		try {
			sessionFactory = new AnnotationConfiguration().configure()
					.buildSessionFactory();
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
}

Course.java :

package com.vaannila.course;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name="COURSES")
public class Course {

    private long courseId;
    private String courseName;
    
    public Course() {
    }
    
    public Course(String courseName) {
    this.courseName = courseName;
    }
    
    @Id
    @GeneratedValue
    @Column(name="COURSE_ID")
    public long getCourseId() {
    	return this.courseId;
    }
    
    public void setCourseId(long courseId) {
    	this.courseId = courseId;
    }
    
    @Column(name="COURSE_NAME", nullable=false)
    public String getCourseName() {
   		return this.courseName;
    }
    
    public void setCourseName(String courseName) {
    	this.courseName = courseName;
    }

}

Main.java :

package com.vaannila.course;

import java.util.List;
import java.util.Iterator;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.vaannila.util.HibernateUtil;

public class Main {

	public static void main(String[] args) {
		Main obj = new Main();
		Long courseId1 = obj.saveCourse("Physics");
		Long courseId2 = obj.saveCourse("Chemistry");
		Long courseId3 = obj.saveCourse("Maths");
		obj.listCourse();

	}
	
	public Long saveCourse(String courseName)
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		Long courseId = null;
		try {
			transaction = session.beginTransaction();
			Course course = new Course();
			course.setCourseName(courseName);
			courseId = (Long) session.save(course);
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return courseId;
	}
	
	public void listCourse()
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			List courses = session.createQuery("from Course").list();
			for (Iterator iterator = courses.iterator(); iterator.hasNext();)
			{
				Course course = (Course) iterator.next();
				System.out.println(course.getCourseName());
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}
	

	

}

Try to use this HibernateUtil

public class HibernateUtil {

    private static final SessionFactory SESSION_FACTORY = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            return new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return SESSION_FACTORY;
    }
}

PETER ,
Sorry for being late! I 've been working on it with your new HibernateUtil class however the folllowing errors troubles me deeply..
Peter any other idea??

15 [main] INFO org.hibernate.cfg.annotations.Version - Hibernate Annotations 3.5.1-Final
31 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.5.1-Final
31 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
46 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
46 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
250 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons Annotations 3.2.0.Final
265 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
265 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
421 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: SESSION_FACTORY
531 [main] INFO org.hibernate.cfg.AnnotationBinder - Binding entity from annotated class: com.vaannila.course.Course
Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.vaannila.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
	at com.vaannila.util.HibernateUtil.<clinit>(HibernateUtil.java:9)
	at com.vaannila.course.Main.saveCourse(Main.java:27)
	at com.vaannila.course.Main.main(Main.java:16)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/Cacheable
	at org.hibernate.cfg.AnnotationBinder.determineCacheSettings(AnnotationBinder.java:946)
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:589)
	at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:636)
	at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:359)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
	at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)
	at com.vaannila.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
	... 3 more
Caused by: java.lang.ClassNotFoundException: javax.persistence.Cacheable
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
	... 10 more

Ahhh this is why I wasn't aware of it (still on 3.3.2 ), there are some additional library now need it. Check this out

Thank you Peter ! I nearly start a survey on Maven to find my lost jars..
Anyway thanks again, Daniweb numberone !

There been some changes with Hibernate 3.5 and 3.6 so in many cases following many online tutorials will get you in trouble. My quick take on the problem:

  • With 3.5 and higher there is no more need for hibernate-annotations dependency as this is now packages as part of hibernate-core (check Hibernate documentation for setup and configuration)
  • With 3.6 AnnotationConfiguration is deprecate and you need to replace it with Configuration
    So previously used HibernateUtil class looking like
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.AnnotationConfiguration;
    
    public class HibernateUtil {
    
        private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
    
        private static SessionFactory buildSessionFactory() {
            try {
                return new AnnotationConfiguration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                throw new ExceptionInInitializerError(ex);
            }
        }
    
        public static SessionFactory getSessionFactory() {
            return SESSION_FACTORY;
        }
    }

    now looks as

    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    public class HibernateUtil {
    
        private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
    
        private static SessionFactory buildSessionFactory() {
            try {
                return new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                throw new ExceptionInInitializerError(ex);
            }
        }
    
        public static SessionFactory getSessionFactory() {
            return SESSION_FACTORY;
        }
    }
  • In case of following error
    java.lang.NoClassDefFoundError: org/hibernate/annotations/common/reflection/MetadataProvider

    make sure that you are not using

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.3.0.ga</version>
    </dependency>

    that is reported as release by mistake with some bugs, but rather use

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>3.2.0.Final</version>
    </dependency>

Thanx a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.