Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails/GORM "in" criteria

Is it possible to do an "in" criteria using the GORM criteria. I'm looking for the equivalent of the following SQL

select * from Person where age in (20,21,22);

If it was possible I guess the syntax would be something like:

def results = Person.withCriteria {
    in "age", [20, 21, 22]
}
like image 525
Dónal Avatar asked Oct 14 '11 21:10

Dónal


People also ask

What is create criteria in Grails?

Description. Criteria queries are a type-safe, advanced way to query that uses a Groovy builder to construct potentially complex queries. It is a much better alternative to using a StringBuilder to dynamically construct an HQL query. Refer to the user guide section on Criteria for usage instructions.

What is Grails Gorm?

GORM is the data access toolkit used by Grails and provides a rich set of APIs for accessing relational and non-relational data including implementations for Hibernate (SQL), MongoDB, Neo4j, Cassandra, an in-memory ConcurrentHashMap for testing and an automatic GraphQL schema generator.

What is hibernate criteria?

Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.


1 Answers

The Grails createCriteria documentation includes an example of using the in clause:

'in'("holderAge",[18..65]) 
or not{'in'("holderAge",[18..65])}

The documentation includes this note:

Note: 'in' is a groovy reserve word, so it must be escaped by quotes.

like image 64
James Allman Avatar answered Oct 19 '22 23:10

James Allman