views:

27

answers:

0

I'm getting an exception when I try to save a batch data (using hibernate as the persistence layer),

Here's the exception:

org.hibernate.exception.DataException: could not insert: [hbm.Employee]

here's my business logic to save the data: (DusinessDAO.class)

public void saveEmployee(Employee employee) {
        Session session = null;
        Transaction tx = null;
        session = HibernateSessionFactory.getSession();
        tx = session.beginTransaction();
        Employee emp;
        if (employee.getEmpcode() != null) {
            System.out.println("Test");
            emp = (Employee) session.get(Employee.class, employee.getEmpcode());
            if (emp != null) {
                System.out.println("Test-2");
                emp.setEmpcode(employee.getEmpcode());
                emp.setEmpfname(employee.getEmpfname());
                emp.setEmplname(employee.getEmplname());
                emp.setEmptelno(employee.getEmptelno());
                emp.setEmpdob(employee.getEmpdob());
                emp.setEmpstatus(employee.getEmpstatus());
                session.update(emp);
        } else {
            System.out.println("Test-3");
            emp = new Employee();
            emp.setEmpcode(employee.getEmpcode());
            emp.setEmpfname(employee.getEmpfname());
            emp.setEmplname(employee.getEmplname());
            emp.setEmptelno(employee.getEmptelno());
            emp.setEmpdob(employee.getEmpdob());
            emp.setEmpstatus(employee.getEmpstatus());
            session.save(emp);
        }
        }
        tx.commit();
    }

Using struts as the controller: (Form bean class)

public class EmployeeEditForm extends ActionForm {

    /**
     * 
     */
    private static final long serialVersionUID = 4448195850351761004L;

    private Employee employee;


    private DateFormat df;

    public String getEmpcode() {
        return employee.getEmpcode();
    }

    public void setEmpcode(String empcode) {
        employee.setEmpcode(empcode);
    }

    public Department getDepartment() {
        return employee.getDepartment();
    }

    public void setDepartment(Department department) {
        employee.setDepartment(department);
    }

    public String getEmpfname() {
        return employee.getEmpfname();
    }

    public void setEmpfname(String empfname) {
        employee.setEmpfname(empfname);
    }

    public String getEmplname() {
        return employee.getEmplname();
    }

    public void setEmplname(String emplname) {
        employee.setEmplname(emplname);
    }

    public Date getEmpdob() {
        return employee.getEmpdob();
    }

    public void setEmpdob(String empdob) {
        Date dt;
        try {
            dt = df.parse(empdob);
            employee.setEmpdob(dt);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public String getEmptelno() {
        return employee.getEmptelno();
    }

    public void setEmptelno(String emptelno) {
        employee.setEmptelno(emptelno);
    }

    public String getEmpstatus() {
        return employee.getEmpstatus();
    }

    public void setEmpstatus(String empstatus) {
        employee.setEmpstatus(empstatus);
    }

    public Date getAuditfield() {
        return employee.getAuditfield();
    }

    public void setAuditfield(Date auditfield) {
        employee.setAuditfield(auditfield);
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public void reset(ActionMapping arg0, HttpServletRequest arg1) {
        employee = new Employee();
    }

}

Action class:

public class EmployeeEditAction extends DispatchAction {
    /*
     * Generated Methods
     */

    /** 
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward saveEmployee(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        EmployeeEditForm employeeEditForm = (EmployeeEditForm) form;// TODO Auto-generated method stub
        BusinessDao businessDao = new BusinessDao();
        businessDao.saveEmployee(employeeEditForm.getEmployee());
        return mapping.findForward("showList");
    }