Rohit Bajaj
asked on
Unable to set up Hibernate
Hi,
I am having the following files in my project. Here i am trying to write a simple java hibernate program to insert rows into a table :
ManageEmployee.java
hibernate.cfg.xml
Employee.java
Now when i execute my ManageEmployee.java file
I get the following exception :
java.lang.NoClassDefFoundE rror: ch/qos/logback/core/joran/ spi/JoranE xception
at line :
factory = new AnnotationConfiguration(). configure( ).addAnnot atedClass( Employee.c lass).buil dSessionFa ctory();
I then added logback-core...jar file
But then i got the following exception :
java.lang.NoClassDefFoundE rror: ch/qos/logback/classic/sel ector/Cont extSelecto r
This is really confusing me. How do i determine what jars will be required to execute this hibernate code.
Also i am new to maven. Please help me in making this simple hibernate application in maven.
Thanks
I am having the following files in my project. Here i am trying to write a simple java hibernate program to insert rows into a table :
ManageEmployee.java
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.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class ManageEmployee {
private static SessionFactory factory;
public static void main(String[] args) {
try{
factory = new AnnotationConfiguration().configure().addAnnotatedClass(Employee.class).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);
/* Delete an employee from the database */
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();
employee.setFirstName(fname);
employee.setLastName(lname);
employee.setSalary(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 employees = session.createQuery("FROM Employee").list();
for (Iterator iterator =
employees.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();
}
}
}
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/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>
<!-- Assume students is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://192.168.32.27/yatra_front_dev
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password"/>
</session-factory>
</hibernate-configuration>
Employee.java
import javax.persistence.*;
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
@Id @GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@Column(name = "salary")
private int salary;
public Employee() {}
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;
}
}
Now when i execute my ManageEmployee.java file
I get the following exception :
java.lang.NoClassDefFoundE
at line :
factory = new AnnotationConfiguration().
I then added logback-core...jar file
But then i got the following exception :
java.lang.NoClassDefFoundE
This is really confusing me. How do i determine what jars will be required to execute this hibernate code.
Also i am new to maven. Please help me in making this simple hibernate application in maven.
Thanks
I then added logback-core...jar fileYou will need to add "logback-classic" JAR to your classpath too. If you downloaded logback from the website, then both should be in the .zip file
ASKER
Hi ,
I added the logback-classic jar in my class path. Now it gives the following exception :
java.lang.NoClassDefFoundE rror: org/slf4j/impl/MessageForm atter
Even though i have included slf4j.jar in classpath
I added the logback-classic jar in my class path. Now it gives the following exception :
java.lang.NoClassDefFoundE
Even though i have included slf4j.jar in classpath
ASKER
Attaching all the jars i have included in my classpath
jars.png
jars.png
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If it does not then you can download it from http://logback.qos.ch/