Top Banner
Advanced Criteria Queries Wednesday Session - 29th, June 2016 Presented by - Nakul
19

Advanced criteria queries

Jan 21, 2018

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced criteria queries

Advanced Criteria QueriesWednesday Session - 29th, June

2016

Presented by - Nakul

Page 2: Advanced criteria queries

Agenda

Criteria Queries Overview

Detached Criteria Queries

Named Queries

Page 3: Advanced criteria queries

Audience

Grails Developer

Page 4: Advanced criteria queries

Criteria VS Detached CriteriaCriteria is created based on hibernate session. For example :-

template.getSessionFactory().getCurrentSession().createCriteria(Person.class);

DetachedCriteria is not dependent on hibernate session. For example :-

DetachedCriteria.forClass(Person.class);

Page 5: Advanced criteria queries

Criteria VS Detached Criteria

Criteria Queries require a session to be present when the query is build up and

fired upon database.

Detached Criteria are criteria queries that are not associated with any given

database session/connection.

Detached Criteria queries have many uses including allowing you to create

common reusable criteria queries, execute subqueries and execute batch

updates/deletes.

Page 6: Advanced criteria queries

Building Detached Criteria

import grails.gorm.*

def criteria = new DetachedCriteria(Person)

To build a normal criteria query you can use the build closure

def criteria = new DetachedCriteria(Person).build {

eq 'lastName', 'Simpson'

}

Page 7: Advanced criteria queries

Executing Detached Criteria

Unlike regular criteria, Detached Criteria are lazy, in that no query is executed at

the point of definition.

list - List all matching entities

get - Return a single matching result

count - Count all matching records

deleteAll - Delete all matching records

updateAll(Map) - Update all matching records with the given properties

Page 8: Advanced criteria queries

Executing Detached Criteriadef criteria = new DetachedCriteria(Person).build {

ilike 'lastName', 'LastName 2%'

projections{

'lastName'

}

}

def results = criteria.list(max: 8, sort: "lastName")

def results1 = criteria.list(max:8, sort:"lastName") {

gt 'age', 24

}

To retrieve a single result you can use the get or find methods (which are synonyms):

Person p = criteria.find() // or criteria.get()

Page 9: Advanced criteria queries

Executing Detached CriteriaThe DetachedCriteria class itself also implements the Iterable interface

which means that it can be treated like a list:

def criteria = new DetachedCriteria(Person).build {

eq 'lastName', 'LastName 2%'

}

criteria.each {

println it.firstName

}

In this case the query is only executed when the each method is called. The same

applies to all other Groovy collection iteration methods.

Page 10: Advanced criteria queries

Executing Detached CriteriaYou can also execute dynamic finders on DetachedCriteria just like on

domain classes. For example:

def criteria1 = new DetachedCriteria(Person).build {

ilike 'lastName', 'LastName 2%'

}

def person = criteria1.findByAge(76)

Page 11: Advanced criteria queries

Batch Operations with Detached CriteriaThe DetachedCriteria class can be used to execute batch operations such as

batch updates and deletes. For example, the following query will update all people

with the surname "Simpson" to have the surname "Bloggs" -

def criteria = new DetachedCriteria(Person).build {

eq 'lastName', 'Simpson'

}

int total = criteria.updateAll(lastName:"Bloggs")

Page 12: Advanced criteria queries

Batch Operations with Detached CriteriaThe DetachedCriteria class can be used to execute batch operations such as

batch updates and deletes. For example, the following query will update all people

with the surname "Simpson" to have the surname "Bloggs" -

def criteria = new DetachedCriteria(Person).build {

eq 'lastName', 'Simpson'

}

int total = criteria.updateAll(lastName:"Bloggs")

Page 13: Advanced criteria queries

Batch Operations with Detached CriteriaTo batch delete records you can use the deleteAll method

def criteria = new DetachedCriteria(Person).build {

eq 'lastName', 'Nexthoughts'

}

int total = criteria.deleteAll()

Page 14: Advanced criteria queries

Named QueriesThe namedQueries static property defines named queries.

Named queries support the criteria builder syntax.

static namedQueries = {

lastNameFilter {

ilike('lastName', 'Next%')

}

}

Page 15: Advanced criteria queries

Executing Named Queries

// Lists All records from person where lastName ilike “Next%” -

List<Person> personList= Person.lastNameFilter.list()

//Get’s the first element

Person.lastNameFilter.get()

//Get total count of records present

Person.lastNameFilter.count()

Page 16: Advanced criteria queries

Executing Named Queries

//Execute list as we did with criteria

Person.lastNameFilter.list(max: 5)

//Apply more criteria within the namedQuerry

Person.lastNameFilter{ lt ('age',65 as Long)}

Page 17: Advanced criteria queries

References

1. The Definitive Guide to Grails 2 - Graeme Rocher & Jeff Scott Brown

2. Programming Grails - Burt Beckwith

3. Grails Documentation - Detached Criteria

4. http://tatiyants.com/how-and-when-to-use-various-gorm-querying-options/

Page 18: Advanced criteria queries

Questions ?

Page 19: Advanced criteria queries

Thank You !