////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////


== withCriteria



=== Purpose


Allows inline execution of Criteria queries. See the link:createCriteria.html[createCriteria] method for reference.


=== Return value


If no matching records are found, an empty List is returned.

If a projection is specified:

* returns a single value if it only contains one field
* a List in case there are multiple fields in the projection

Otherwise, it will return a List of matched instances of the class calling withCriteria.


=== Examples


[source,groovy]
----
def results = Book.withCriteria {
    def now = new Date()
    between('releaseDate', now-7, now)
    like('title', '%Groovy%')
}
----


=== Description


The `withCriteria` method allows the inline definition of  link:{hibernate5GuideFromRef}index.html#criteria[Criteria]. Arguments to the [https://apache.github.io/grails-data-mapping/latest/api/grails/orm/HibernateCriteriaBuilder.html] can be passed as the first parameter:

[source,groovy]
----
def book = Book.withCriteria(uniqueResult: true) {
    def now = new Date()
    between('releaseDate', now-7, now)
    like('title', '%Groovy%')
}
----

Parameters:

* `arguments` (optional) - A map of named arguments to be set on the Criteria instance
* `closure` - A closure that defines the criteria
