Hibernate返回两个Pojo

Hibernate返回两个Pojo

本文介绍了Hibernate返回两个Pojo,并使用jstl在jsp上显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的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:

DateRange:

public class DateRange extends RecordSet {
    private String  ddate;

    public String getDdate() {
        return ddate;
    }

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

RecordSet:

RecordSet:

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:

I have my 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)的结果

My Requirement is to display the results from both(Master and DateRange) in a jsp using JSTL Tags

任何人都可以如何获取最终的List对象以迭代并显示来自两个pojo的结果

Could any one How can I get final List object to iterate and display results from both pojo's

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

I want to display EmployeeName,Code,Dept from Master Pojo and ddate from DateRange Pojo?

当我遍历RecordSet列表时,我无法打印这些值(获取Null Pointer Exception)?

when I iterate the RecordSet list then I'm not able to print the values(getting Null Pointer Exception)?

推荐答案

您可以执行以下操作:

<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.

And the return the recordset object to the jsp.

希望这会有所帮助.

这篇关于Hibernate返回两个Pojo,并使用jstl在jsp上显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:23