简体   繁体   中英

Retrieving the first item from the list and converting it in int

I have the following class as shown below

public class BrokerInvoiceLineItem {


    private int attachmentCount;

public int getAttachmentCount() {
        return attachmentCount;
    }

    public void setAttachmentCount(int attachmentCount) {
        this.attachmentCount = attachmentCount;
    }
}

the named query in xml is

<sql-query name="attachmentQuery">
     <![CDATA[select count(iilnmp.INV_LINE_NOTE_ID) from IOA_INV_LINE_NOTE_MAP iilnmp , 
                                                IOA_INVOICE_LINE_NOTES iiln , IOA_INVOICE_LINE iil
                                               where   iilnmp.INV_LINE_NOTE_ID = iiln.ID and iiln.INLI_ID =iil.id and iil.ID = ?]]>
</sql-query>

now below is the operation that i am doing where from a list at index 0 i am retrieving the value but i am getting an compilation error as i need to cast the value stored at index 0 into int type then only i will be able to set please advise how can i achieve the same

Query query = session.getNamedQuery("attachmentQuery");
                query.setParameter(0, itrBrokerInvoiceLineItem.getId());
                List attachCount = query.list();
                if (attachCount != null && attachCount.size() > 0) {
                    if (attachCount.get(0) != null) {
                        itrBrokerInvoiceLineItem.setAttachmentCount(attachCount.get(0));
                    }
                }

the compilation error that i am getting is The method setAttachmentCount(int) in the type BrokerInvoiceLineItem is not applicable for the arguments (Object)

I have edited my question please as i am using hibernate 3.1 in which query.getsingleresult method is not there please advise

Replace

List attachCount = query.list();

With this

List<Integer> attachCount = (List<Integer>) query.list();

If i understand your question you want 1st result after executing your query. So, i will prefer you to use query.getSingleResult() with handling exception rather than query.list() . Which will return Object .


(After Edit) it seems that you want count based on some parameters. So i will prefer you to change in name query with this

<sql-query name="attachmentQuery">
     <![CDATA[select * from IOA_INV_LINE_NOTE_MAP iilnmp , 
                                                IOA_INVOICE_LINE_NOTES iiln , IOA_INVOICE_LINE iil
                                               where   iilnmp.INV_LINE_NOTE_ID = iiln.ID and iiln.INLI_ID =iil.id and iil.ID = ?]]>
</sql-query>

And then use query.list().getSize() which will return int

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