Grails:没有方法的签名findAll()适用于参数类型:String,ArrayList [英] Grails: No signature of method findAll() is applicable for argument types: String, ArrayList

查看:1547
本文介绍了Grails:没有方法的签名findAll()适用于参数类型:String,ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是grails新手,并收到以下错误:

方法没有签名:Something.findAll()适用于参数类型:(java.lang.String ,java.util.ArrayList)values:[from Something AS s WHERE s.some_number LIKE ?, [%asdf%]]



运行 test-app 时会发生错误,它发生在以下位置:

  SomethingVO [] findBySomeNumber(String searchString){
searchString =%+ searchString +%
return Something.findAll(From something AS s WHERE s.some_number LIKE?, searchString])。collect {
new SomethingVO(it);
}
}

Something 是一个域对象:

 包some.project.domain 

类某些{

static belongsTo = [product:product,productVersion:ProductVersion]

Long id
字符串名称
Str一些数字

静态约束= {
product(可空值:true)
productVersion(可空值:true)
}
}

错误在哪里?

(我使用Grails 1.2.4 )

解决方案

findAll在单元测试期间没有被模拟,这就是为什么你的代码不工作。在运行测试之前,您需要手动添加一个模拟调用(mockFor可以帮助你)。这适用于如果您使用HQL或Criteries(我建议使用纯HQL)。

或者,您可以使用动态查找器解决您的问题。当您在单元测试中调用 mockDomain(Something)时,动态查找程序和其他动态ORM方法(save,get,count,..) 。它们通常比HQL(imho)更易于使用。

更新:
感谢Fletch指出并非所有动态的发现者被嘲笑。一个不会被嘲笑的动态查找器的例子是: Something.findAllWhereSomeNumberInList([1,2,3])



您在代码中使用的HQL可以使用动态查找器像这样重写:

  Something.findBySomeNumberLike searchString)


I'm new to grails and receive the following error:
No signature of method: Something.findAll() is applicable for argument types: (java.lang.String, java.util.ArrayList) values: [from Something AS s WHERE s.some_number LIKE ?, [%asdf%]]"

The error occurs when I run test-app. It occurs in the following place:

SomethingVO[] findBySomeNumber(String searchString) {
     searchString = "%"+searchString+"%"
     return Something.findAll("from Something AS s WHERE s.some_number LIKE ?",[searchString]).collect { 
          new SomethingVO(it);    
     }
}  

The class Something is a domain object:

package some.project.domain

    class Something{

        static belongsTo = [product:Product, productVersion:ProductVersion]

        Long id
        String name
        String someNumber

        static constraints = {
            product (nullable:true)
            productVersion (nullable:true)
        }
    }  

Where is the mistake?

(I use Grails 1.2.4)

解决方案

findAll is not mocked during unit testing and that's why your code isn't working. You need to manually add a mock for the call before running your test (mockFor could help you with that). This applies if your use HQL or Criterias (which I would recommend over pure HQL).

Alternatively it's possible that you could solve your problems using dynamic finders. Dynamic finders and the other dynamic ORM methods (save, get, count, ..) are in most(?) cases mocked when you call mockDomain(Something) in your unit test. They are also generally easier to use than HQL (imho).

Update: Thanks to Fletch for pointing out that not all dynamic finders are mocked. An example of a dynamic finder that won't be mocked is this: Something.findAllWhereSomeNumberInList([1, 2, 3]).

The HQL you use in your code could be rewritten like this using dynamic finders:

Something.findBySomeNumberLike(searchString)

这篇关于Grails:没有方法的签名findAll()适用于参数类型:String,ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆