简体   繁体   中英

retrieve value from One-To-Many relationship Hibernate Without HQL

I just try to retrieve a value from One-To-Many relationship in 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". here is the structure of the table Picture of ER

I need to get 'name' from staff table who assigned to centre by searching centre.

Here is my POJO files

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

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

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.

 <%                    
                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??

**

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

<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());

                             }


%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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