-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Executing a hibernate file
PostPosted: Fri Apr 12, 2013 6:10 am 
Newbie

Joined: Fri Apr 12, 2013 1:53 am
Posts: 18
I wrote a hibernate program in netBeans version 7.3. I created the required files
- hibernate.cfg.xml
-hibernate.reveng.xml
-POJO class -> Employee.java
-mapping class -> Employee.hbm.xml
-main class ->ManageEmployee.java


I'm getting the following exceptions:
at firstproject.ManageEmployee.main(ManageEmployee.java:16)
Caused by: java.lang.ClassNotFoundException: Employee
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
at org.hibernate.mapping.PersistentClass.getMappedClass(PersistentClass.java:96)
... 9 more
Java Result: 1


Top
 Profile  
 
 Post subject: Re: Executing a hibernate file
PostPosted: Fri Apr 12, 2013 6:14 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
can u show ur cfg file mapping to hbm seems to me hibernate is unable to find the pojo.

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
 Post subject: Re: Executing a hibernate file
PostPosted: Fri Apr 12, 2013 10:21 am 
Newbie

Joined: Fri Apr 12, 2013 1:53 am
Posts: 18
the following is the code of 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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mynewdatabase?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<mapping resource="hibernateproject/employees.hbm.xml"/>
</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: Executing a hibernate file
PostPosted: Fri Apr 12, 2013 12:00 pm 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
2 things first your hbm must be declare in the same package as relative classes Its actually by the convention.
And Secondly check your hbm class tag the value provided should be like a
Configfolder/packagae/class.

Try tweaking out or check the mapping example at my blog if it's still an option issue share all code here

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
 Post subject: Re: Executing a hibernate file
PostPosted: Sat Apr 13, 2013 12:15 am 
Newbie

Joined: Fri Apr 12, 2013 1:53 am
Posts: 18
The hibernate.cfg.xml and hibernate.reveng.xml are in default package under the Source Package.
The Employee.hbm.xml, Employee.java & ManageEmployee.java are in the firstproject package under Source Package.

The Employee.hbm.xml code is as follows:
<?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="Employee" table="employee">
<meta attribute="class-description"> This class contains the employee detail. </meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>


The Employee.java code is as follows:
package firstproject;
public class Employee
{
private int id;
private String firstName;
private String lastName;
private int salary;
public Employee() {}
public Employee(String fname, String lname, int salary)
{
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}
public int getId()
{
return id;
}
public void setId( int id )
{
this.id = id;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName( String first_name )
{
this.firstName = first_name;
}
public String getLastName()
{
return lastName;
}
public void setLastName( String last_name )
{
this.lastName = last_name;
}
public int getSalary()
{
return salary;
}
public void setSalary( int salary )
{
this.salary = salary;
}
}

The ManageEmployee code is as follows:
package firstproject;
import java.util.List;
import java.util.Date;
import java.util.Iterator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ManageEmployee
{
public static SessionFactory factory;
public static void main(String[] args)
{
try{
factory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex)
{
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
ManageEmployee ME = new ManageEmployee();
/* Add few employee records in database */
Integer empID1 = ME.addEmployee("Zara", "Ali", 1000);
Integer empID2 = ME.addEmployee("Daisy", "Das", 5000);
Integer empID3 = ME.addEmployee("John", "Paul", 10000);
/* List down all the employees */
ME.listEmployees();
/* Update employee's records */
ME.updateEmployee(empID1, 5000);
ME.deleteEmployee(empID2);
/* List down new list of the employees */
ME.listEmployees();
}
/* Method to CREATE an employee in the database */
public Integer addEmployee(String fname, String lname, int salary)
{
Session session = factory.openSession();
Transaction tx = null;
Integer employeeID = null;
try{
tx = session.beginTransaction();
Employee employee = new Employee(fname, lname, salary);
employeeID = (Integer) session.save(employee);
tx.commit();
}catch (HibernateException e)
{
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally
{
session.close();
}
return employeeID;
}
/* Method to READ all the employees */
public void listEmployees( )
{
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
List employ = session.createQuery("FROM Employee").list();
for (Iterator iterator =
employ.iterator(); iterator.hasNext();){
Employee employee = (Employee) iterator.next();
System.out.print("First Name: " + employee.getFirstName());
System.out.print(" Last Name: " + employee.getLastName());
System.out.println(" Salary: " + employee.getSalary());
}
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
/* Method to UPDATE salary for an employee */
public void updateEmployee(Integer EmployeeID,int salary )
{
Session session = factory.openSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
Employee employee =(Employee)session.get(Employee.class, EmployeeID);
employee.setSalary( salary );
session.update(employee);
tx.commit();
}catch (HibernateException e)
{
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally
{
session.close();
}
}
/* Method to DELETE an employee from the records */
public void deleteEmployee(Integer EmployeeID){
Session session = factory.openSession();
Transaction tx = null;
try{
tx = session.beginTransaction();
Employee employee =(Employee)session.get(Employee.class, EmployeeID);
session.delete(employee);
tx.commit();
}catch (HibernateException e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
}
}


Top
 Profile  
 
 Post subject: Re: Executing a hibernate file
PostPosted: Sat Apr 13, 2013 12:52 am 
Regular
Regular

Joined: Wed Apr 10, 2013 1:02 am
Posts: 50
<mapping resource="hibernateproject/employees.hbm.xml"/>
to <mapping resource="firstproject/employees.hbm.xml"/>


<class name="firstproject.Employee" table="employee">
to
<class name="Employee" table="employee">

_________________
Regards
Akash Miital
http://akash.thegrassroots.co.in/hibernate/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.