0

I have a Hibernate Code like below which returns single Entity class:

SQLQuery query = 
                session.createSQLQuery("select * from Employee");
            query.addEntity(Employee.class);
                       List<Employee> emplist= query.list();

for above code my Jstl working code is:

<c:choose>
<c:when test="${requestScope.size!=0}">
<table border="1">
<tr>
<th>
EmployeeName
</th>
<th>EmployeeDepartment</th>

</tr>
<c:forEach items="${requestScope.absentlist}" var="emps">
<tr>
<td><c:out value="${emps.empno}"/></td>
<td><c:out value="${emps.empname}"/></td>

</tr>


</c:forEach>

But My Requirement is to write a jstl code For a Hibernate which is Returning two Entities

Here is my Hibernate Code:

SQLQuery query = session
                    .createSQLQuery("CALL AbsentReportproc(:_fromdate,:_todate)");
            query.addEntity(Master.class);
            query.addEntity(DateRange.class);
            query.setParameter("_fromdate", "2012-12-03");
            query.setParameter("_todate", "2012-12-04");

            List<RecordSet> obj = query.list();     
            Iterator it=obj.iterator();

             while(it.hasNext())
                {
                    Object[] obj1 = (Object[]) it.next();
                    for (int i = 0; i < obj1.length; i++) {

                        RecordSet set=(RecordSet)obj1[i];
                        if(set instanceof Master){
                         System.out.println("Employee Code"+((Master) set).getEmpcode());
                         System.out.println("Employee Dept"+((Master) set).getDept());
                         System.out.println("Employee Name"+((Master) set).getEmpname());                   
                        }
                        if(set instanceof DateRange){
                         System.out.println("AbsentDate"+((DateRange) set).getDdate());
                        }
                    }
                }

Could any one provide me the JSTL Code to display the above results on jsp page?

String
  • 3,660
  • 10
  • 43
  • 66
  • So, is it a `List` or a `List`? What should be displayed? How? – JB Nizet Feb 04 '13 at 10:10
  • It's not clear from your question why the JSTL you've provided wouldn't work for both cases. Can you post the code for your backing object? – Derek Feb 04 '13 at 10:11
  • when I called the list() method the I'm avialble with Object array items which is having obj[0]={Master obj,DateRange obj} etc..... – String Feb 04 '13 at 10:13
  • check this http://stackoverflow.com/questions/14659209/java-arraylist-iteration-having-a-classcastexception – String Feb 04 '13 at 10:15
  • I have used but forgot to mention public class Master extends RecordSet and public class DateRange extends RecordSet in the above link – String Feb 04 '13 at 10:16

2 Answers2

2

So you in fact have a List<Object[]>, and each Object[] in the list contains an instance of Master as its first element, and an instance of DateRange as its second element.

Start by writing a class which contains those two objects, which will make your code easier to read:

public class MasterAndDateRange {
    private Master master;
    private DateRange dateRange;
    // constructor and getters omitted for brevity
}

Then iterate through your List<Object[]> and create a List<MasterAndDateRange>.

Then store this list in a request attribute ("listOfMasterAndDateRange", for example).

Then, in the JSP, use the JSTL to iterate over this list:

<c:forEach var="masterAndDateRange" items="listOfMasterAndDateRange">
   ...
   <c:out value="${masterAndDateRange.master.foo}"/>
   ...
   <c:out value="${masterAndDateRange.dateRange.bar}"/>
   ...
</c:forEach>
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Could you give me the Sample code How Could i iterate please,I had tried but facing type casting exception – String Feb 06 '13 at 05:07
  • Object list = query.list(); Iterator it=list.iterator(); while(it.hasNext()){ MasterAndDateRange obj1 = (MasterAndDateRange)it.next(); } – String Feb 06 '13 at 05:14
  • I had tried but facing type casting exception Object cannot be cast to MasterAndDateRange,Could you provide me the Sample code please – String Feb 06 '13 at 05:15
  • Re-read the first 2 sentences of the answer: you have a List. It's up to you to transform this List into a List. by iterating through the List, transforming each Object[] into a MasterDateRange instance, and adding then to a new List. – JB Nizet Feb 06 '13 at 15:39
  • No, I can't. It's your job to provide the code. Iterating on a list isn't something you should be afraid of. – JB Nizet Feb 06 '13 at 20:29
  • Could you tell me my Iteration logic was correct or not? in this link http://stackoverflow.com/questions/14743952/hibernate-returning-two-pojos-and-display-the-results-on-jsp-using-jstl – String Feb 07 '13 at 08:44
  • when i execute the logic in the above link I'm not able to get the values – String Feb 07 '13 at 08:45
0

AS per your code you are calling stored procedure and not just single table query. Hence hibernate will return the Object list only.

You needs to iterate the loop and populate the required object.

Other option is as suggested by @JB Nizet.

Manoj Kathiriya
  • 3,366
  • 5
  • 19
  • 19