1

I know that dyanmic finders in grails only support 2 parameters eg.

artifacts = Artifact.findAllByDocumentAndArtifactType(document,artifactType,[max:limit, offset:startIndex]);

So to use more than 3 arguments I found this example and it works. However I need to use the max and offset operators.

artifacts = Artifact.findAllWhere(document:document,artifactType:artifactType,status:null,[max:limit, offset:startIndex]);

This however returns the entire list and not the limit asked for. Does the operator work for findAllwhere? How to use it or limit my findings withing a certain range?

Community
  • 1
  • 1
krs8785
  • 1,097
  • 2
  • 14
  • 24
  • Why did you say that you grails only support 2 parameters ? From the documentation: "Method expressions can also use a boolean operator to combine two or more criteria". What is your version of Grails ? – Abincepto Oct 17 '14 at 15:22
  • 2.4.1. It did before but I think it is discontinued – krs8785 Oct 17 '14 at 15:25
  • 1
    Sounds strange. You should try this dynamic finders: Artifact.findAllByDocumentAndArtifactTypeAndStatusIsNull(document, artifactType, [max:limit,offset:startIndex]) – Abincepto Oct 17 '14 at 15:39
  • I just added this comment as an answer for the others users. Could be you please validate my answer ? – Abincepto Oct 17 '14 at 16:40

2 Answers2

1

You can use dynamic finders with more than two parameters. You should try this dynamic finders:

Artifact.findAllByDocumentAndArtifactTypeAndStatusIsNull(document, artifactType, [max:limit,offset:startIndex]) 

Hope that helps

Abincepto
  • 1,016
  • 8
  • 13
  • We changed this in 2.0 to allow an unlimited number of args as long as they're all combined with "And" or all "Or". The restriction was artificial, just trying to keep method names from getting out of control. But there were several requests, so we decided that if you want absurdly long names, go for it. And if you want less chaos in your life, use `findWhere`, Criteria queries, "where" queries, or HQL. – Burt Beckwith Oct 18 '14 at 00:31
  • Thanks for the recommendation. On my side, my limit is usually 3. Is there a loss in performance using long dynamic finders ? – Abincepto Oct 18 '14 at 06:04
  • Runtime performance is not the problem with bad method names like `findAllByDocumentAndArtifactTypeAndStatusIsNull`. Development time performance is the problem. A human has to look very carefully at that method name in order to determine what the criteria is. – Jeff Scott Brown Oct 18 '14 at 12:56
1

I found a more graceful solution that doesn't require typing out the super monolithicly long dynamic helper.

You can use findAll with closures, like this:

artifacts = Artifact.findAll([max:limit, offset:startIndex]) {
      document == document
      artifactType == artifactType
      status == null
}
kokorohakai
  • 215
  • 2
  • 7