简体   繁体   English

从一对多关系中检索值无需HQL即可休眠

[英]retrieve value from One-To-Many relationship Hibernate Without HQL

I just try to retrieve a value from One-To-Many relationship in Hibernate. 我只是尝试从Hibernate中的“一对多”关系中检索一个值。 But I stuck with getting values. 但是我坚持获取价值。

here is the scenario - There are centre and staff. 这是方案-有中心和工作人员。 the centre just like Branch of office. 中心就像办公室的分支。 and it has a staff member assigned to centre one staff member can assign more centres. 并且有一名职员分配到中心,一名职员可以分配更多的中心。

There is three table called "centres", "staff" and "centerhasstaff". 有三个表,分别称为“中心”,“工作人员”和“ centerhasstaff”。 here is the structure of the table Picture of ER 这是表格的结构ER图片

I need to get 'name' from staff table who assigned to centre by searching centre. 我需要从通过搜索中心分配给中心的职员表中获取“姓名”。

Here is my POJO files 这是我的POJO文件

Center.java Center.java

 private Integer idCenter;
 private Branch branch;
 private String centerName;
 private String centerDay;
 private String centertime;
 private String isApprove;
 private String isActive;
 private String createdBy;
 private String centerNo;
 private Set centerHasStaffs = new HashSet(0);

CenterHasStaff.java CenterHasStaff.java

 private Integer idcenterHasStaff;
 private Center center;
 private Staff staff;

Staff.java Staff.java

private Integer idStaff;
 private String nic;
 private String licenceNo;
 private String name;
 private String address1;
 private String address2;
 private String city;
 private Set centerHasStaffs = new HashSet(0);

here is how I try to retrieve Staff name on JSP Page. 这是我尝试在JSP页面上检索人员名称的方法。

 <%                    
                int i = 1;

                Session ss = DB.getSession();
                Criteria crr = ss.createCriteria(Center.class);
                crr.add(Restrictions.eq("isActive", "Active"));
                crr.add(Restrictions.eq("isApprove", "Approve"));

                List<Center> li = crr.list();

                for (Center el : li) { %>

            <tr>
                <th scope="row"><%= i++ %></th>
                <td><%= el.getCenterNo() %></td>
                <td><%= el.getCenterName() %></td>
                <td><%= el.getCenterDay() %></td>
                <td><%= el.getCentertime() %></td>
                <td><%= el.getCenterHasStaffs() %></td>
                <td><%= el.getCreatedBy() %></td>
                <td>OK</td>
            </tr>

            <%  }%>

Here is my resultset like 这是我的结果集

Image of Resultset 结果集的图片

** **

Is there way to get staff name assigned to centre without using HQL?? 有没有办法在不使用HQL的情况下将职员姓名分配给中心?

** **

Looking at the resultset and object graph, this is returning you a list back 查看结果集和对象图,这将返回一个列表

<td><%= el.getCenterHasStaffs() %></td>

Assuming the center has 1 staff assigned, then you can grab the first item from the list 假设中心分配了1名员工,那么您可以从列表中获取第一项

<td><%= el.getCenterHasStaffs().get(0).getName() %></td>

I Just found Solution for my problem. 我刚刚找到了解决我的问题的方法。 Is this better way to do that? 这是更好的方法吗?

<%

        Iterator it = el.getCenterHasStaffs().iterator();

        while (it.hasNext()) {

        CenterHasStaff elem = (CenterHasStaff) it.next();
        out.print(elem.getStaff().getNameWithinitials());

                             }


%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM