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]
}
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With