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();
}
}
}