【问题标题】:Display values retrieved from database in a table在表中显示从数据库中检索的值
【发布时间】:2017-10-23 06:12:41
【问题描述】:

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

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

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

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

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 查询

【问题讨论】:

  • 您需要创建一个结果,它是一个列表,其中包含一个包含员工信息任务的对象。目前您有两个不相关的列表
  • 您完全忽略了 Hibernate 的要点,即将关系数据库映射到对象模型,在实体之间具有关联(而不是 ID)。您的查询应该只检索员工,JSP 应该显示employee.name、employee.role.title,并遍历employee.tasks 以显示员工每项任务的描述。因此,Employee 应该与 Role 具有 ManyToOne 关联,并与 Task 具有 OneToMany 或 ManyToMany 关联。
  • 我对休眠很陌生。那么,您能否提供一个关于我到底哪里出错的答案示例。提前致谢
  • 一切都已经在我的评论中了。现在你需要开始阅读一本书,或者只是阅读 Hibernate 的用户手册来开始学习关联。 docs.jboss.org/hibernate/orm/current/userguide/html_single/…

标签: java sql hibernate foreach controller


【解决方案1】:

代替 List employeeTaskList 使用 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;
}

【讨论】:

  • 非常感谢 Tejal 的回复。但是由于&lt;td&gt;是根据第一个列表动态创建的,我应该怎么做employeeTaskList.set...这里?
  • 在上面的 for 循环中创建 Employee emp=new Employee() 并在对象中设置值并将其添加到 employeeTaskList 中
【解决方案2】:

这两个列表不相关,正如 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>

希望这会有所帮助。

【讨论】:

  • 谢谢阿克谢。这正是我想要做的,我可以看到这应该有效。但是当我尝试这个时,没有显示任何任务。因此,我尝试测试resultTaskList 返回的结果数,结果为 0。由于查询在以 sql 运行时有效,因此我无法找到它不起作用的原因。任何帮助将不胜感激。
  • 查看这个答案并检查您是否使用了正确的驱动程序。 stackoverflow.com/a/6841177/3270795
猜你喜欢
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
相关资源
最近更新 更多