在我的grails项目中,我使用了Object.findAllByIdInList()
方法,将列表作为参数传递。
使用的代码如下:def allSelectedIds = ReceiptItem.findAllByIdInList(par)
其中ReceiptItem是定义如下的域类:
class Receipt {
double totalAmount;
Date releaseDate;
int vatPercentage;
int discount;
Boolean isPayed;
Boolean isInvoice;
static belongsTo = [patient:Patient]
static hasMany = [receiptItems:ReceiptItem]
static constraints = {
receiptItems(blank: false)
patient(blank: false)
totalAmount(blank: false)
vatPercentage(blank: false, nullable: false)
}
}
par
是定义如下的ID列表:def par = params.list("receiptItemsSelected")
在gsp页面中的
receiptItemsSelected
中定义了remoteFunction()
,如下所示:params: '\'receiptItemsSelected=\' + jQuery(this).val()'
问题是上面的行引发以下异常:
java.lang.String cannot be cast to java.lang.Long. Stacktrace follows:
Message: java.lang.String cannot be cast to java.lang.Long
我不明白为什么会引发此异常。
谢谢你的帮助
最佳答案
列表par
可能具有ids
作为String
。通常,域对象的id
存储为Long
。试试这个ReceiptItem.findAllByIdInList(par*.toLong())
*还要确保id
表示为字符串isNumber()
。
assert !'C'.isNumber()
assert '4'.isNumber()
关于grails - grails:findallbyidinlist方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17367879/