使用JPQL从Object中检索列表

时间:2013-09-11 10:40:03

标签: java jpql

我想从proposalInsuredPersonList对象中检索lifeProposal。但我得到了此异常proposal.ProposalInsuredPerson cannot be cast to java.util.List。让我知道如何检索List

            query.append("SELECT f.id, f.submittedDate, f.proposalNo, f.customer, f.organization, pi.proposedPremium, pi.proposedSumInsured, s.date, " +
                    "f.agent, f.proposalInsuredPersonList FROM LifeProposal f,LifeSurvey s INNER JOIN f.proposalInsuredPersonList pi where f.id = s.lifeProposal.id and f.id is not null");

            objectList = q.getResultList();
            for(Object[] b : objectList) {
                porposalId = (String) b[0];
                proposalDate = (Date) b[1];
                porposalNo = (String) b[2];
                if(b[4] == null) {
                    Customer c = (Customer) b [3];
                    customerName =  c.getFullName();
                    customerAddress =   c.getFullAddress();
                    fatherName = c.getFatherName();
                    phNo = c.getContentInfo() == null ? "" : c.getContentInfo().getPhone() ;
                } else {
                    Organization org = (Organization) b[4];
                    customerName = org.getName();
                    customerAddress = org.getFullAddress();
                    fatherName = null;
                    phNo = org.getContentInfo() == null ? "" : org.getContentInfo().getPhone() ;
                }
                inspectionDate = (Date) b[7];
                premium = (Double) b[5];
                sumInsured = (Double) b[6];
                if(b[8] == null) {
                    agentName = "";
                    agentNo = "";
                } else {
                    Agent a = (Agent) b[8];
                    agentName = a.getName();
                    agentNo = a.getCodeNo();
                }
                insuredPersonList = (List<ProposalInsuredPerson>)  b[9];

2 个答案:

答案 0 :(得分:0)

好的,根据您在此处显示的代码,我会尽可能地回答。 循环遍历结果集,并在返回的对象的索引9处收到ProposalInsuredPerson对象。 由于您需要整个列表,我建议将此对象添加到List实现中(ArrayList会想到),该实现在for循环之外被声明和实例化。

这样的事情可以解决你的问题:

列出insuredPersonList = new ArrayList();

for () {
// your code here

// ...
  insuredPersonList.add((ProposalInsuredPerson) b[9]);
}

答案 1 :(得分:0)

您是否尝试过这样重写您的查询:

query.append("SELECT f.id, f.submittedDate, f.proposalNo, f.customer, f.organization, pi.proposedPremium, pi.proposedSumInsured, s.date, " +
                    "f.agent, f FROM LifeProposal f,LifeSurvey s INNER JOIN f.proposalInsuredPersonList pi where f.id = s.lifeProposal.id and f.id is not null");

即。将f.proposalInsuredPersonList替换为整个f

并在最后一行执行此操作:

insuredPersonList = ((LifeProposal) b[9]).getProposalInsuredPersonList();