Grails find () using 3 fields / columns (i.e. find with multiple columns)

I am a bit confused about the Grails HQL documentation. Say I have a domain class with 3 fields,

class Example { String a String b String c } 

and I want to make the equivalent of Example.findByAAndBAndC (), can you show me the line of code that I would use to set this and pass in 3 parameters? Note that the documentation says that you can use two fields for findBy. I need to make 3 fields.

thanks

+4
source share
3 answers

AFAIK, you cannot use dynamic crawlers if you have more than two predicates. Use the criteria query instead:

 def results = Example.withCriteria { eq('a', 'some-a') eq('b', 'some-b') eq('c', 'some-c') } 

Update

By default, predicates are concatenated using AND if you want to use OR instead:

 def results = Example.withCriteria { or { eq('a', 'some-a') eq('b', 'some-b') eq('c', 'some-c') } } 
+9
source

I use in my project:

 Example.findAllByAIlikeAndBIlikeAndCIlike("${a}%","${b}%","${c}%",[max:5, offset:0, sort:"a", order:"asc"]) 

and it works!

+5
source

Ray,

There are several ways to “find” objects.

findWhere:

 Example.findWhere(a:"Hello", B:"World") Example.findByAandB("a value", "b value) 

I would advise you to read well:

http://grails.org/doc/latest/guide/ and
http://grails.org/doc/latest/guide/single.html#5.4.2%20Criteria

The documentation is not bad and has some examples.

+4
source

Source: https://habr.com/ru/post/1381393/


All Articles