简体   繁体   中英

Retrieve List from Object with JPQL

I want to retrieve proposalInsuredPersonList from lifeProposal Object .But i got this exception proposal.ProposalInsuredPerson cannot be cast to java.util.List . Let me know how can i retrieve 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];

Ok, I'll answer as far as I can, based on the code you show here. You loop over your resultset and at index 9 of the returned objects you receive a ProposalInsuredPerson object. Since you want the whole list, I recommend adding this object to a List implementation ( ArrayList comes to mind) which is declared and instantiated outside of your for loop.

So something like that will fix your problem:

List insuredPersonList = new ArrayList();

for () {
// your code here

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

Have you tried rewriting your query like this:

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

ie replacing f.proposalInsuredPersonList with whole f

and the on the last line do this:

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

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