Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Hibernate Annotation difficulty
PostPosted: Sat May 26, 2012 3:10 pm 
Newbie

Joined: Sat May 26, 2012 2:58 pm
Posts: 1
Hi,

I am just trying to compile a simple Hibernate example using Annotations.

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

}



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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/UR_DEV</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.password">system</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-->mapping resource="com/vaannila/course/Course.hbm.xml"/-->
<mapping resource="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;
}
}


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();
obj.updateCourse(courseId3, "Mathematics");
obj.deleteCourse(courseId2);
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();
}
}

public void updateCourse(Long courseId, String courseName)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Course course = (Course) session.get(Course.class, courseId);
course.setCourseName(courseName);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}

public void deleteCourse(Long courseId)
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
Course course = (Course) session.get(Course.class, courseId);
session.delete(course);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}


I am getting the following error when compiling Main.java

110 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.3.0.GA
110 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
125 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
141 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
282 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
282 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
485 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/vaannila/course/Course
Initial SessionFactory creation failed.org.hibernate.MappingNotFoundException: resource: com/vaannila/course/Course not found
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: org.hibernate.MappingNotFoundException: resource: com/vaannila/course/Course not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:596)
at org.hibernate.cfg.AnnotationConfiguration.parseMappingElement(AnnotationConfiguration.java:212)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1589)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1568)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1542)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1448)
at com.vaannila.util.HibernateUtil.<clinit>(HibernateUtil.java:10)
... 2 more



Please Help!!!!!!!!!!!!!


Top
 Profile  
 
 Post subject: Re: Hibernate Annotation difficulty
PostPosted: Thu Jun 28, 2012 1:49 am 
Newbie

Joined: Thu Jun 07, 2012 2:07 am
Posts: 5
hello


just change ur <mapping resource=""/>
in ur hibernate.cfg.xml

with


<mapping class="where your class exists" />



thats it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.