Hibernate返回两个Pojo并使用jstl在jsp上显示结果

时间:2013-02-07 05:07:27

标签: java spring hibernate jstl hibernate-mapping

我有我的pojo课程

站长:

public class Master extends RecordSet{

private String empcode;
private String empname;
private String dept;

public String getEmpcode() {
    return empcode;
}
public void setEmpcode(String empcode) {
    this.empcode = empcode;
}
public String getEmpname() {
    return empname;
}
public void setEmpname(String empname) {
    this.empname = empname;
}
public String getDept() {
    return dept;
}
public void setDept(String dept) {
    this.dept = dept;
}

DATERANGE:

public class DateRange extends RecordSet {
    private String  ddate;

    public String getDdate() {
        return ddate;
    }

    public void setDdate(String ddate) {
        this.ddate = ddate;
    }
}

记录集:

public class RecordSet {
    private DateRange daterange;
    private Master master;  

public DateRange getDaterange() {
        return daterange;
    }
    public void setDaterange(DateRange daterange) {
        this.daterange = daterange;
    }
    public Master getMaster() {
        return master;
    }
    public void setMaster(Master master) {
        this.master = master;
    }

}

我有我的HibernateCode:

 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<Object[]> obj = query.list();
            System.out.println(obj);
            List<RecordSet> recordset = new ArrayList<RecordSet>();
            Iterator it = obj.iterator();
            while (it.hasNext()) {
                Object[] object = (Object[]) it.next();
                for (int i = 0; i < object.length; i++) {

    RecordSet set = (RecordSet) object[i];
     recordset.add(set);
        System.out.println(set);
//Here I am able to print
//edu.model.Master@15093f1
//edu.model.DateRange@e6f7d2
//edu.model.Master@19836ed
//edu.model.DateRange@3e0ebb
//edu.model.Master@39443f
//edu.model.DateRange@e6f7d2 etc................

                }
            }

    }

我的要求是使用JSTL标签在jsp中显示(Master和DateRange)的结果

可以任何一个如何获得最终的List对象来迭代并显示来自pojo的

的结果

我想显示Master Pojo的EmployeeName,Code,Dept和DateRange Pojo的ddate?

当我迭代RecordSet列表然后我无法打印值(获得空指针异常)?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

<c:forEach var="record" items="${recordset}">
    <c:out value="${record.master.empcode}" />
    <c:out value="${record.master.empname}" />
    <c:out value="${record.master.dept}" />
    <c:out value="${record.daterange.ddate}" />
</c:forEach>

编辑

List<Object[]> obj = query.list(); 
List<RecordSet> recordset = new ArrayList<RecordSet>(); 
for (Object[] item: obj) {
    Master master = null;
    DateRange dateRange = null;
    if (item[0] instanceof Master) {
        Master master = (Master) item[0];
        DateRange dateRange = (DateRange) item[1];
    } else if (item[0] instanceof DateRange) {
        DateRange dateRange = (DateRange) item[0];
        Master master = (Master) item[1];
    }
    RocordSet record = new RecordSet();
    record.setMaster(master);
    record.setDateRange(dateRange);
    recordset.add(record);
}

并将记录集对象返回给jsp。

希望这有帮助。