iberck
2/11/2017 - 3:27 AM

GORM Query Where

GORM Query Where

Queries Where

Se introdujeron en Grails 2. Aunque con where podemos hacer los mismo que con dinamic finders, estos son definitivamente más potentes.

  • No permite decir el tipo de fetch de las colecciones.
  • Utiliza por debajo DetachedCriteria, el siguiente ejemplo retorna un DetachedCriteria:
Product.where {
    manufacturer == ACME && (salesPrice > 200 && salesPrice < 800)
}

List

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()

Order by

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

Unique

Si no encuentra ningún registro, retorna null.

def query = Abono.where {
    eq("id", "1")
}
Abono abono = query.get()

Count

@Transactional(readOnly = true)
long countNotificacionesSinLeer(Hotel hotel) {
    def count = Notify.where {
        eq("hotel", hotel)
    }.count()

    return (long) count
}