【发布时间】:2016-05-09 10:47:55
【问题描述】:
我有如下的类,如下图
public class BrokerInvoiceLineItem {
private int attachmentCount;
public int getAttachmentCount() {
return attachmentCount;
}
public void setAttachmentCount(int attachmentCount) {
this.attachmentCount = attachmentCount;
}
}
xml中的命名查询是
<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>
现在下面是我正在执行的操作,我从索引 0 处的列表中检索值,但我收到编译错误,因为我需要将存储在索引 0 处的值转换为 int 类型,然后只有我会能够设置 请告知我怎样才能达到同样的效果
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 method setAttachmentCount(int) in the type BrokerInvoiceLineItem is not applicable for the arguments (Object)
我已经编辑了我的问题,因为我使用的是休眠 3.1,其中没有 query.getsingleresult 方法,请告知
【问题讨论】:
-
错误到底在哪里?什么是堆栈跟踪?
-
命名查询长什么样?
-
您已经创建了一个原始类型的
List(类型Object),并且您尝试将一个值存储在int类型中。这是不允许的。我不知道Query.list()返回什么值,但尝试使用菱形运算符 (List<Integer>) 声明您的列表。