0

我正在尝试在这里注册一个新员工。问题是这些值没有被插入到数据库中。虽然 save() 保存值的工作对吗?我在这里附上我的代码。

package net.admin.module.controller;
import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;





import net.admin.module.model.Employee;
import net.admin.module.util.HibernateUtil;

public class EmployeeManager extends HibernateUtil {

    public Employee add(Employee employee) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(employee);
        session.getTransaction().commit();
        return employee;
    }
    public Employee delete(int id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Employee employee = (Employee) session.load(Employee.class, id);
        if(null != employee) {
            session.delete(employee);
        }
        session.getTransaction().commit();
        return employee;
    }


        public List<Employee> list() {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Employee> employee = null;
        try {

            employee = (List<Employee>)session.createQuery("from Employee").list();

        } catch (HibernateException e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }
        session.getTransaction().commit();
        return employee;
    }
    }

这是我的 EmployeeManager.java 类。它是我的控制器。

package net.admin.module.view;

import java.util.List;
import net.admin.module.controller.EmployeeManager;
import net.admin.module.model.Employee;
import com.opensymphony.xwork2.ActionSupport;

public class EmployeeAction extends ActionSupport {

    private static final long serialVersionUID = 9149826260758390091L;
    private Employee employee;
    private List<Employee> employeeList;
    private int id;

    private EmployeeManager employeeManager;

    public EmployeeAction() {
        employeeManager = new EmployeeManager();
    }

    public String execute() {
        this.employeeList = employeeManager.list();
        System.out.println("execute called");
        return SUCCESS;
    }



    public String add() {
        System.out.println(getEmployee());
        try {
            employeeManager.add(getEmployee());
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.employeeList = employeeManager.list();
        return SUCCESS;
    }


    public String delete() {
        employeeManager.delete(getId());
        return SUCCESS;
    }

    public Employee getEmployee() {
        return employee;
    }

    public List<Employee> getEmployeeList() {
        return employeeList;
    }

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

    public void setEmployeeList(List<Employee> employeeList) {
        this.employeeList = employeeList;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

这是我的 EmployeeAction.java 类。这是我的行动课。

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>Employee Manager</title>
</head>
<style>
body
 {
 background:Teal;
 }
 </style>
<body>

<h1>Employee Manager</h1>
<s:actionerror/>

<s:form action="add" method="post">
    <s:textfield name="employee.id" label="Id" />
    <s:textfield name="employee.firstName" label="Firstname"/>
    <s:textfield name="employee.lastName" label="Lastname"/>
    <s:textfield name="employee.email" label="Email"/>
    <s:textfield name="employee.phoneNo" label="Phone No."/>
    <s:textfield name="employee.presentAddress" label="Present Address"/>
    <s:textfield name="employee.permanentAddress" label="Permanent Address" />
    <s:textfield name="employee.passportNo" label="Passport No." />
    <s:textfield name="employee.panCardNo" label="Pan Card No." />
    <s:textfield name="employee.designation" label="Designation"/>
    <s:textfield name="employee.age" label="Age" />
    <s:textfield name="employee.gender" label="Gender" />
    <s:textfield name="employee.dob" label="Dob" />
    <s:textfield name="employee.status" label="Status" />
    <s:textfield name="employee.createdOn" label="Created On" />
    <s:textfield name="employee.updatedOn" label="Updated On" />
    <s:textfield name="employee.role" label="Role" />
    <s:textfield name="employee.joiningDate" label="Joining Date" />
    <s:submit value="Add Employee" align="center"/>
</s:form>


<h2>Employee</h2>
<table>
<tr>
    <th>Id</th>
    <th>FirstaName</th>
    <th>LastName</th>
    <th>Email</th>
    <th>Phone No.</th>
    <th>Present Address</th>
    <th>Permanent Address</th>
    <th>Passport No.</th>
    <th>Pan Card No.</th>
    <th>Designation</th>
    <th>Age</th>
    <th>Gender</th>
    <th>Dob</th>
    <th>Status</th>
    <th>Created On</th>
    <th>Updated On</th>
    <th>Role<th>
    <th>Joining Date</th>
    <th>Delete</th>
</tr>
<s:iterator value="employeeList" var="employee">
    <tr>
        <td><s:property value="id"/>
        <s:property value="firstName"/> </td>
        <td><s:property value="lastName"/> </td>
        <td><s:property value="emailId"/></td>
        <td><s:property value="phoneNo"/></td>
        <td><s:property value="presentAddress"/></td>
        <td><s:property value="permanentAddress"/></td>
        <td><s:property value="passportNo"/></td>
        <td><s:property value="panCardNo"/></td>
        <td><s:property value="designation"/></td>
        <td><s:property value="age"/></td>
        <td><s:property value="gender"/></td>
        <td><s:property value="dob"/></td>
        <td><s:property value="status"/></td>
        <td><s:property value="createdOn"/></td>
        <td><s:property value="updatedOn"/></td>
        <td><s:property value="role"/></td>
        <td><s:property value="joiningDate"/></td>
        <td><a href="delete?id=<s:property value="id"/>">delete</a></td>
    </tr>

</s:iterator>
</table> 
</body>

</html>

这是我的 jsp - index.jsp 。我在这里转发。我可以在 jsp 页面中看到插入的值,但它没有插入到数据库中。

4

0 回答 0