显示从表中的数据库检索的值

时间:2017-10-23 06:12:42

标签: java sql hibernate foreach controller

我是hibernate框架的新手,我正在netbeans IDE中开发一个Hibernate框架项目。我有一个连接到项目的javadb,我需要获取每个员工的任务列表,并将其显示为<td>中的每个员工姓名的项目符号列表。以下是显示员工的表格。使用EmployeeHelper类中的getEmployeeDetails方法获取的名称和角色。

  

问题
- 虽然我调用从Controller传递的resultTaskList参数,但任务列表始终为空。
- 我知道我必须对if进行if条件检查在嵌套的forloop中,将列出根据员工姓名对任务进行分组的任务。但我不确定如何在jsp页面中执行if

有关如何在同一个表格中显示每位员工的任务列表的任何建议都将受到高度赞赏。

在此表中,我打算将每个员工的相关任务作为每个相应<td>内的项目符号列表。

Table

Employee.jsp

...
<div class="content">
<!--Display table of roles with an edit button against each role-->
<form method="get" action="<%= request.getContextPath() %>/RoleController">
    <br>

    <table border ="2">
        <thead class="thead-inverse">
            <tr>
              <th></th>
              <th>Employee Name</th>
              <th>Role</th>
              <th>Tasks</th>
              <th>Edit</th>
            </tr>
          </thead>
          <tbody>
            <c:forEach items="${result}" var="res">
                <tr>
                    <th scope="row">
                    </th>
                    <td>
                        <c:out value="${res[0]}"></c:out>
                    </td>
                    <td>
                        <c:out value="${res[1]}"></c:out>
                    </td>
                    <td>
                        <c:out value="${resultTaskList[1]}"></c:out>
                    </td>
                    <td>
                        <input type="button" class="btn btn-info" value="Edit Role">
                    </td>
                </tr>
            </c:forEach>
    </table>
</form>
</div>
...

EmployeeController.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    EmployeeHelper helper = new EmployeeHelper();
    List<Employee> resultTaskList = helper.getEmployeeTasks();
    request.setAttribute("resultTaskList", resultTaskList);
    List<Employee> result = helper.getEmployeeDetails();
    request.setAttribute("result", result);
    request.getRequestDispatcher("Employee.jsp").forward(request, response);
}

EmployeeHelper.java

public class EmployeeHelper {
    Session session = null;

    public EmployeeHelper(){
        this.session = HibernateUtil.getSessionFactory().openSession();
    }


    public List getEmployeeDetails(){
        Transaction tx = this.session.beginTransaction();

        List<Employee> employeeList = null;

        try{
            Query query = session.createQuery("select e.name, r.title from Employee as e, Role as r where e.employeeid=r.employeeid");
            employeeList = (List<Employee>)query.list();
            tx.commit();
        }
        catch(HibernateException ex){
            ex.printStackTrace();
            tx.rollback();
        }
        return employeeList;
    }

    public List getEmployeeTasks(){
        Transaction tx = this.session.beginTransaction();

        List<Employee> employeeTaskList = null;

        try{
            Query query = session.createQuery("select e.name, t.description from Employee as e, Role as r, Task as t, EmployeeTask as et where e.employeeid=r.employeeid and t.taskid=et.taskid and e.employeeid=et.employeeid");
            employeeTaskList = (List<Employee>)query.list();
            tx.commit();
        }
        catch(HibernateException ex){
            ex.printStackTrace();
            tx.rollback();
        }
        return employeeTaskList;
    }
}

getEmployeeTasks()方法

中执行的SQL查询

image

2 个答案:

答案 0 :(得分:1)

而不是List employeeTaskList使用List和iterate循环并在employee list中设置值尝试以下代码。

   public List getEmployeeTasks(){

    Transaction tx = this.session.beginTransaction();

    List<Employee> employeeTaskList = null;

    try{
       session.beginTransaction();
      Query query = session.createQuery("select e.name, t.description from Employee as e, Role as r, Task as t, EmployeeTask as et where e.employeeid=r.employeeid and t.taskid=et.taskid and e.employeeid=et.employeeid");
        List<Object[]> result = (List<Object[]>) query.list();

    if (null != result && result.size() != 0) {

            for (Object[] obj : result) {
                //Set values over here 
                employeeTaskList.set.....
            }
        tx.commit();
    }
    }
    catch(HibernateException ex){
        ex.printStackTrace();
        tx.rollback();
    }
    return employeeTaskList;
}

答案 1 :(得分:0)

JB Nizet 建议这两个列表不相关。你仍然可以做这样的事情。

        <c:forEach items="${result}" var="res">
            <tr>
                <th scope="row">
                </th>
                <td>
                    <c:out value="${res[0]}"></c:out>
                </td>
                <td>
                    <c:out value="${res[1]}"></c:out>
                </td>
                <td>
                <c:forEach items="${resultTaskList}" var="resultTaskList">
                    <c:if test = "${res[0] == resultTaskList[0]}">
                    <c:out value="${resultTaskList[1]}"></c:out>
                    </c:if>
                </c:forEach>
                </td>
                <td>
                    <input type="button" class="btn btn-info" value="Edit Role">
                </td>
            </tr>
        </c:forEach>

希望这有帮助。