GORM Query Where
Se introdujeron en Grails 2. Aunque con where podemos hacer los mismo que con dinamic finders, estos son definitivamente más potentes.
DetachedCriteria, el siguiente ejemplo retorna un DetachedCriteria:Product.where {
manufacturer == ACME && (salesPrice > 200 && salesPrice < 800)
}
list() retorna la lista de objetos encontrados, si no encuentra ningún objeto retorna una lista vacía [].
def query = Product.where {
manufacturer == ACME && (salesPrice > 200 && salesPrice < 800)
}
query.list()
def query = Person.where {
(lastName != "Simpson" && firstName != "Fred") || (firstName == "Bart" && age > 9)
}
def results = query.list(sort:"firstName", order:"asc")
La propiedad sort y order se traducen como order by firstName asc
Si no encuentra ningún registro, retorna null.
def query = Abono.where {
eq("id", "1")
}
Abono abono = query.get()
@Transactional(readOnly = true)
long countNotificacionesSinLeer(Hotel hotel) {
def count = Notify.where {
eq("hotel", hotel)
}.count()
return (long) count
}