I am trying to build a simple form and i'm using only hibernate. plz help me out i'm getting this error ================================== first.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Employee Registry</title> </head> <body> <h1>Employee Registration</h1> <form action="/EmServl.do" method="post"> <p>${message}</p> <table cellpadding="3pt" border=0> <tr> <td>First Name</td> <td><input type="text" size="30" name="First"/></td> </tr> <tr> <td>Last Name</td> <td><input type="text" size="30" name="Last"/></td> </tr> <tr> <td>Position</td> <td><input type="text" size="30" name="Position"/></td> </tr> <tr> <td>Salary</td> <td><input type="text" size="30" name="Salary"/></td> </tr> </table>
<table cellpadding="3pt" border=0> <tr> <td><input type="submit" value="Add"/></td> </tr> </table>
</form> </body> </html>
================================================
HibernateUtil.java
package com.tut.hibernate.buildclass;
import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration;
public class HibernateUtil { private static final SessionFactory sessionfactory; static { try { sessionfactory = new Configuration().configure().buildSessionFactory();
}catch (Throwable ex){ System.err.println("Initial Session Factory Creation failed" +ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionfactory; } } =======================================================
Employee.java
package com.tut.hibernate.modelclass;
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.TableGenerator;
@Entity public class Employee { @Id @TableGenerator(initialValue=1 ,name="de", allocationSize=1) @GeneratedValue(strategy = GenerationType.TABLE,generator = "de") private int employeeID; private String firstName, lastName, position, salary;
public int getEmployeeID() { return employeeID; } public void setEmployeeID(int employeeID) { this.employeeID = employeeID; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; }
}
=============================================
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "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> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hiber</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">jithern</property> <property name="hbm2ddl.auto">create </property> <property name="show_sql">true</property> <mapping class="com.tut.hibernate.modelclass.Employee" /> </session-factory> </hibernate-configuration>
=====================================================================
EmServl.java
package com.tut.hibernate.saveclass;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import com.tut.hibernate.modelclass.Employee; import com.tut.hibernate.sessclass.EmployeeM1;
/** * Servlet implementation class EmServl */ @WebServlet("/EmServl") public class EmServl extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public EmServl() { super(); // TODO Auto-generated constructor stub }
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub Employee e = new Employee(); e.setFirstName(request.getParameter("First")); e.setLastName(request.getParameter("Last")); e.setPosition(request.getParameter("Position")); e.setSalary(request.getParameter("Salary")); EmployeeM1 p = new EmployeeM1(); p.addEmp(e); String message = "Your Data Inserted"; request.setAttribute("message", message); RequestDispatcher dispatcher = request.getRequestDispatcher("/first.jsp"); dispatcher.forward(request,response); }
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub }
}
===========================================
EmployeeM1.java
package com.tut.hibernate.sessclass;
import org.hibernate.Session; import org.hibernate.Transaction;
import com.tut.hibernate.buildclass.HibernateUtil; import com.tut.hibernate.modelclass.Employee;
public class EmployeeM1 { public void addEmp(Employee a) { Transaction transaction = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { transaction = session.beginTransaction(); session.save(a); session.getTransaction().commit(); }catch(RuntimeException e) { if(transaction != null){ transaction.rollback(); } e.printStackTrace(); }finally{ session.flush(); session.close(); } }
}
======================================================
|