grails - grails:findallbyidinlist方法

标签 grails methods controller

在我的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/

相关文章:

mysql - 为什么 truncateTable 在 Joomla 3.7 中不起作用?

c# - boolean 值作为方法参数不改变状态

ruby-on-rails - Ruby on Rails - 发送联系表格 - Controller 问题

ruby-on-rails - 博客条目和评论的倒序显示,Ruby on Rails

grails - grails 2.3.0 Controller 中保存操作的重定向和响应如何工作

eclipse - 无法使用Grails 2.2.1在GGTS 3.2中运行单元测试

grails - 获取行时验证失败

validation - 验证Grails中的加密密码

java - 方法未执行

spring - NullpointerException:无法获取空对象的属性