• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Grails object relational mapping: GORM
 

Grails object relational mapping: GORM

on

  • 3,369 views

GORM: All about relationships in between domain objects in Grails, and how GROM handle them

GORM: All about relationships in between domain objects in Grails, and how GROM handle them

Statistics

Views

Total Views
3,369
Views on SlideShare
3,369
Embed Views
0

Actions

Likes
6
Downloads
51
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Grails object relational mapping: GORM Grails object relational mapping: GORM Presentation Transcript

    • By: Saurabh Dixit
    •  Automatically maps domain objects todatabase Provides query and update facility1. Finding objects in persistence store(findby methods)2. Persistence life cycle methods3. Criteria and HQL query facilities
    •  Grails injects GORM functionality into objectsin run time Declare properties for the domain class1.The id and version properties are injectedautomatically.2. All properties are not null/required bydefault.
    •  Associations One‐to‐one, one‐to‐many, many‐to‐many Uni‐directional and bi‐directional Owners defined by using belongsToproperty
    •  Many-to-one relationship: A Many-to-one relationship is thesimplest kind, and is defined with aproperty of the type of another domainclass. Consider this example: A unidirectional many-to-onerelationship from Face to Nose: class Face {Nose nose}class Nose {}
    •  To make this relationship bidirectional definethe other side as follows:class Face {Nose nose}class Nose {static belongsTo = [face:Face]}
    •  The result of this is that we can create a Face,attach a Nose instance to it and when wesave or delete the Face instance, GORM willsave or delete the Nose. In other words, savesand deletes will cascade fromFace to the associated Nose:Face f = new Face()f.nose = new Nose()f.save()
    •  one-to-one Relationship: use the hasOne property on the owningside, e.g. Face:classAuthor {static hasOne = [book:Book]}class Book {Author author} Note that using this property puts theforeign key on the inverse table to theprevious example, so in this case theforeign key column is stored in the nosetable inside a column called face_id.
    •  hasOne only works with bidirectionalrelationships. its a good idea to add a unique constraint onone side of the one-to-one relationship:classAuthor {static hasOne = [book:Book]static constraints = {book unique: true}}class Book {Author author}
    •  One-to-many Relationship:classAuthor {static hasMany = [books: Book]String name}class Book {String title} In this case we have a unidirectional one-to-many. Grails will, by default, map this kind ofrelationship with a join table.
    •  Grails will automatically inject a property oftype java.util.Set into the domain class basedon the hasMany setting.This can be used toiterate over the collection:def a = Author.get(1)for (book in a.books) {println book.title}
    •  The default fetch strategy used by Grails is"lazy", which means that the collection will belazily initialized on first access. The default cascading behaviour is to cascadesaves and updates, but not deletes unless abelongsTo is also specified:classAuthor {static hasMany = [books: Book]String name}class Book {static belongsTo = [author: Author]String title}
    •  If you have two properties of the same typeon the many side of a one-to-many you haveto use mappedBy to specify which thecollection is mapped:classAirport {static hasMany = [flights: Flight]static mappedBy = [flights: "departureAirport"]}class Flight {Airport departureAirportAirport destinationAirport}
    •  Many-to-many Relationship: Grails supports many-to-many relationships bydefining a hasMany on both sides of the relationshipand having a belongsTo on the owned side of therelationship:class Book {static belongsTo = Authorstatic hasMany = [authors:Author]String title}classAuthor {static hasMany = [books:Book]String name}
    •  Grails maps a many-to-many using a jointable at the database level.The owning sideof the relationship, in this case Author, takesresponsibility for persisting the relationshipand is the only side that can cascade savesacross.
    •  Relationship with the basic collection type: As well as associations between differentdomain classes, GORM also supportsmapping of basic collection types. For example, the following class creates anicknames association that is a Set of Stringinstances:class Person {static hasMany = [nicknames: String]}
    •  GORM will map an association like the aboveusing a join table. You can alter various aspects of how the jointable is mapped using the joinTableargument:class Person {static hasMany = [nicknames: String]static mapping = {hasMany joinTable: [name: bunch_o_nicknames,key: person_id,column: nickname,type: "text"]}}
    •  As well as association, Grails supports the notionof composition. In this case instead of mapping classes ontoseparate tables a class can be "embedded" withinthe current table. For example:class Person {Address homeAddressAddress workAddressstatic embedded = [homeAddress, workAddress]}classAddress {String numberString code}
    •  The resulting mapping would looking likethis:If you define the Address class in a separateGroovy file in the grails-app/domaindirectory you will also get an address table.
    •  Custom mappings are defined using a staticmapping block defined within your domainclass:class Person {…static mapping = {version falseautoTimestamp false}}
    •  You can also configure global mappings inConfig.groovy (or an external config file)using this setting:grails.gorm.default.mapping = {version falseautoTimestamp false}
    • class Person {String firstNameSting addressstatic mapping = {version falseautoTimestamp falsetable people //Table namefirstName column: First_Name // column nameaddress type:text}} for the type in mapping you can create userdefined types using hibernateorg.hibernate.usertype Interface UserType.
    •  GORM supports inheritance both fromabstract base classes and concrete persistentGORM entities. Inheritance hierarchies Table‐per‐hierarchy:All classes map to asingle table Table‐per‐subclass: Each class maps to itsown table and JOINs are used
    •  For example:classContent {String author}class BlogEntry extends Content {URL url}class Book extends Content {String ISBN}class PodCast extends Content {byte[] audioStream} In the above example we have a parentContent class and then various child classeswith more specific behavior.
    •  At the database levelGrails by default usestable-per-hierarchy mapping with adiscriminator column called class so theparent class (Content) and its subclasses(BlogEntry, Book etc.), share the same table. By default same table, forTable-per-hierarchy.
    •  Table-per-hierarchy mapping has a down sidein that you cannot have non-nullableproperties with inheritance mapping. To have two different tables for them: usetable-per-hierarchy=false To use table per subclass:▪ tablePerHierarchy false
    •  Retrieving Objects: get(), getAll(), read() Listing objects: list(), listOrderBy*() methods order, sort, offset, and max namedarguments
    •  Sets: Sets of Objects By default when you define a relationshipwithGORM it is a java.util.Set which is anunordered collection that cannot containduplicates. In other words when you have:classAuthor {static hasMany = [books: Book]}
    •  The books property that GORM injects is ajava.util.Set. Sets guarantee uniquenes butnot order, which may not be what you want.To have custom ordering you configure theSet as a SortedSet:class Author {SortedSet booksstatic hasMany = [books: Book]}
    •  In the above case a java.util.SortedSetimplementation is used which means youmust implement java.lang.Comparable inyour Book class:class Book implements Comparable {String titleDate releaseDate = new Date()int compareTo(obj) {releaseDate.compareTo(obj.releaseDate)}}
    •  Lists of Objects: To keep objects in the order which they wereadded and to be able to reference them by indexlike an array you can define your collection typeas a List:classAuthor {List booksstatic hasMany = [books: Book]} In this case when you add new elements to thebooks collection the order is retained in asequential list indexed from 0 so you can do:author.books[0] // get the first book
    •  Bags of Objects: If ordering and uniqueness arent a concern(or if you manage these explicitly) then youcan use the Hibernate Bag type to representmapped collections. The only change required for this is to definethe collection type as a Collection:
    • classAuthor {Collection booksstatic hasMany = [books: Book]} Since uniqueness and order arent managedby Hibernate, adding to or removing fromcollections mapped as a Bag dont trigger aload of all existing instances from thedatabase, so this approach will performbetter and require less memory than using aSet or a List.
    •  Maps of Objects: If you want a simple map of string/value pairsGORM can map this with the following:classAuthor {Map books // map of ISBN:book names}def a = newAuthor()a.books = ["1590597583":"Grails Book"]a.save() In this case the key and value of the mapMUST be strings.
    •  If you want a Map of objects then you can dothis:class Book {Map authorsstatic hasMany = [authors: Author]}def a = new Author(name:"Stephen King")def book = new Book()book.authors = [stephen:a]book.save()
    •  CollectionTypes and Performance: The Java Set type doesnt allow duplicates.Toensure uniqueness when adding an entry to aSet association Hibernate has to load theentire associations from the database. If you have a large numbers of entries in theassociation this can be costly in terms ofperformance.
    •  The same behavior is required for List types,since Hibernate needs to load the entireassociation to maintain order.Therefore it isrecommended that if you anticipate a largenumbers of records in the association thatyou make the association bidirectional sothat the link can be created on the inverseside.
    •  For example consider the following code:def book = new Book(title:"New Grails Book")def author =Author.get(1)book.author = authorbook.save() In this example the association link is beingcreated by the child (Book) and hence it is notnecessary to manipulate the collectiodirectlyresulting in fewer queries and more efficientcode.
    •  Given an Author with a large number ofassociated Book instances if you were towrite code like the following you would seean impact on performance:def book = new Book(title:"New Grails Book")def author =Author.get(1)author.addToBooks(book)author.save() You could also model the collection as aHibernate Bag as described above.
    •  By default Lazy fetching, object will be loadedonly when it will be accessed. Lazy can be set to false to enable eager fetching:classAirport {String namestatic hasMany = [flights: Flight]static mapping = {flights lazy: false}} In this case the flights association will beloaded at the same time as its Airportinstance
    •  Although eager fetching is appropriate forsome cases, it is not always desirable. If you made everything eager you could quitepossibly load your entire database intomemory resulting in performance andmemory problems. An alternative to eager fetching is to usebatch fetching.You can configure Hibernateto lazily fetch results in "batches".
    •  For example:classAirport {String namestatic hasMany = [flights: Flight]static mapping = {flights batchSize: 10}} In this case, due to the batchSize argument,when you iterate over the flights association,Hibernate will fetch results in batches of 10.
    •  For example: If you had an Airport that had 30 flights, if you didnt configure batch fetching youwould get 1 query to fetch the Airport andthen 30 queries to fetch each flight: With batch fetching you get 1 query to fetchthe Airport and 3 queries to fetch eachFlight in batches of 10.
    •  In other words:batch fetching is an optimization of thelazyfetching strategy. Batch fetching can also be configured at theclass level as follows:class Flight {…static mapping = {batchSize 10}}
    •  Optimistic Locking: By default GORM classes are configured foroptimistic locking. Optimistic locking is a feature of Hibernatewhich involves storing a version value in aspecial version column in the database that isincremented after each update.
    •  The version column gets read into a versionproperty that contains the current versionedstate of persistent instance which you canaccess:def airport = Airport.get(10)println airport.version When you perform updates Hibernate willautomatically check the version property againstthe version column in the database and if theydiffer will throw a StaleObjectException.This willrollback the transaction if one is active. The version will only be updated afterflushing the session.
    •  Dealing with the locking through exceptionhandling:def airport = Airport.get(10)try { airport.name = "Heathrow"airport.save(flush: true)}catch (org.springframework.dao.OptimisticLockingFailureException e) {// deal with exception} The way you deal with the exception dependson the application.You could attempt aprogrammatic merge of the data or go backto the user and ask them to resolve theconflict.
    •  Pessimistic Locking: Pessimistic locking is equivalent to doing aSQL "SELECT * FOR UPDATE" statementand locking a row in the database. This has the implication that other readoperations will be blocking until the lock isreleased.
    •  In Grails pessimistic locking is performed on anexisting instance with the lock method:def airport = Airport.get(10)airport.lock() // lock for updateairport.name = "Heathrow"airport.save() Grails will automatically deal with releasing thelock for you once the transaction has beencommitted. In the above case what we are doing is"upgrading" from a regular SELECT to aSELECT..FOR UPDATE and another thread couldstill have updated the record in between the call toget() and the call to lock().
    •  To get around this problem you can use thestatic lock method that takes an id just likeget:def airport = Airport.lock(10) // lock for updateairport.name = "Heathrow"airport.save()
    •  Using pessimistinc locking in dynamic findersand criteria: As well as the lock method you can also obtain apessimistic locking using queries. For example using a dynamic finder:def airport = Airport.findByName("Heathrow", [lock: true]) Or using criteria:def airport = Airport.createCriteria().get {eq(name, Heathrow)lock true}
    •  You can use the isDirty method to check ifany field has been modified:def airport = Airport.get(10)assert !airport.isDirty()airport.properties = paramsif (airport.isDirty()) {// do something based on changed state} isDirty() does not currently check collectionassociations, but it does check all otherpersistent properties and associations.
    •  You can also check if individual fields havebeen modified:def airport = Airport.get(10)assert !airport.isDirty()airport.properties = paramsif (airport.isDirty(name)) {// do something based on changed name}
    •  getDirtyPropertyNames: You can use the getDirtyPropertyNamesmethod to retrieve the names of modifiedfields; this may be empty but will not benull:def airport = Airport.get(10)assert !airport.isDirty()airport.properties = paramsdef modifiedFieldNames = airport.getDirtyPropertyNames()for (fieldName in modifiedFieldNames) {// do something based on changed value}
    •  getPersistentValue: You can use the getPersistentValue methodto retrieve the value of a modified field:def airport = Airport.get(10)assert !airport.isDirty()airport.properties = paramsdef modifiedFieldNames = airport.getDirtyPropertyNames()for (fieldName in modifiedFieldNames) {def currentValue = airport."$fieldName"def originalValue = airport.getPersistentValue(fieldName)if (currentValue != originalValue) {// do something based on changed value}}
    •  Use property names of the class Support for many expressions in findermethoddef book = Book.findByTitle("The Stand")book = Book.findByTitleLike("Harry Pot%") Like is comparator, due to the Likecomparator, above finder is equivalent to aSQL like expression.
    •  The possible comparators include:InList In the list of given valuesLessThan less than a given valueLessThanEquals less than or equal a give valueGreaterThan greater than a given valueGreaterThanEquals greater than or equal a given valueLike Equivalent to a SQL like expressionIlike Similar to a Like, except case insensitiveNotEqual Negates equalityBetween Between two values (requires twoarguments)IsNotNull Not a null value (doesnt take an argument)IsNull Is a null value (doesnt take an argument)
    •  Between, IsNotNull and IsNull requiredifferent numbers of method argumentscompared to the rest, as demonstrated in thefollowing example:def now = new Date()def lastWeek = now - 7def book = Book.findByReleaseDateBetween(lastWeek, now)books = Book.findAllByReleaseDateIsNull()books = Book.findAllByReleaseDateIsNotNull()
    •  Boolean logic (AND/OR): Method expressions can also use a booleanoperator to combine two or more criteria:def books = Book.findAllByTitleLikeAndReleaseDateGreaterThan("%Java%", new Date() - 30) In this case were using And in the middle ofthe query to make sure both conditions aresatisfied, but you could equally use Or:def books = Book.findAllByTitleLikeOrReleaseDateGreaterThan("%Java%", new Date() - 30)
    •  Built on Hibernate’s CriteriaAPI Groovy builder is used to build up the criteria▪ Hierarchy of method calls and closures forbuilding tree‐like structures Each domain class has a createCriteria()method▪ Call get, list, scroll, or count on criteria▪ The criteria builder is used to restrict thequery Supports associations and projections
    •  Some Criteria queries:def c = Account.createCriteria()def results = c.list {like("holderFirstName", "Fred%")and {between("balance", 500, 1000)eq("branch", "London")}maxResults(10)order("holderLastName", "desc")}
    •  Below is a node reference for each criterionmethod:Node Description Examplebetween Where the property value isbetween two distinct valuesbetween("balance", 500, 1000)eq Where a property equals aparticular value.eq("branch", "London")eq(caseinsensitive)A version of eq that supports anoptional 3rd Map parameter tospecify that the query becaseinsensitive.eq("branch", "london",[ignoreCase: true])eqProperty Where one property must equalanothereqProperty("lastTx", "firstTx")
    • Node Description Examplegt Where a property is greater than a particularvaluegt("balance",1000)gtProperty Where a one property must be greater thananothergtProperty("balance","overdraft")ge Where a property is greater than or equal to aparticular valuege("balance", 1000)geProperty Where a one property must be greater than orequal to anothergeProperty("balance","overdraft")idEq Where an objects id equals the specified value idEq(1)ilike A case-insensitive like expression ilike("holderFirstName","Steph%")in where a property is not contained within thespecified list of values.Note: in is a Groovyreserve word,so it must be escaped by quotes.in("age",[18..65]) or not{in("age",[18..65])}
    • isEmpty Where a collection property is empty isEmpty("transactions")isNotEmpty Where a collection property is notemptyisNotEmpty("transactions")isNotEmpty Where a collection property is notemptyisNotEmpty("transactions")isNull Where a property is null isNull("holderGender")isNotNull Where a property is not null isNotNull("holderGender")lt Where a property is less than aparticular valuelt("balance", 1000)ltProperty Where a one property must be lessthan anotherltProperty("balance","overdraft")le Where a property is less than orequal to a particular valuele("balance", 1000)leProperty Where a one property must be lessthan or equal to anotherleProperty("balance","overdraft")
    • like Equivalent to SQL like expression like("holderFirstName","Steph%")ne Where a property does not equal aparticular valuene("branch", "London")neProperty Where one property does not equal another neProperty("lastTx", "firstTx")order Order the results by a particular property order("holderLastName","desc")rlike Similar to like, but uses a regex.Onlysupported on Oracle and MySQL.rlike("holderFirstName",/Steph.+/)sizeEq Where a collection propertys size equals aparticular valuesizeEq("transactions", 10)sizeGt Where a collection propertys size isgreater than a particular valuesizeGt("transactions", 10)sizeGe Where a collection propertys size isgreater than or equal to a particular valuesizeGe("transactions", 10)
    • sizeLt Where a collection propertys size isless than a particular valuesizeLt("transactions", 10)sizeLe Where a collection propertys size isless than or equal to a particular valuesizeLe("transactions", 10)sizeNe Where a collection propertys size isnot equal to a particular valuesizeNe("transactions", 10)sqlRestriction Use arbitrary SQL to modify theresultsetsqlRestriction"char_length(first_name) = 4"
    •  With dynamic finders, you have access tooptions such as max, sort, etc. These are available to criteria queries as well,but they have different names:Name Description Exampleorder(String,String)Specifies both the sort column (thefirst argument) and the sort order(either asc or desc).order "age", "desc"firstResult(int) Specifies the offset for the results. Avalue of 0 will return all records up tothe maximum specified.firstResult 20maxResults(int) Specifies the maximum number ofrecords to return.maxResults 10cache(boolean) Indicates if the query should becached (if the query cacheis enabled).cache true
    •  Criteria also support the notion of projections. A projection is used to change the nature of theresults. For example the following query uses aprojection to count the number of distinctbranch names that exist for each Account:def c = Account.createCriteria()def branchCount = c.get {projections {countDistinct "branch"}}
    •  The following table summarizes the differentprojections and what they do:Name Description Exampleproperty Returns the given property inthe returned resultsproperty("firstName")distinct Returns results using a singleor collection of distinctproperty namesdistinct("fn") ordistinct([fn, ln])avg Returns the average value ofthe given propertyavg("age")count Returns the count of the givenproperty namecount("branch")countDistinct Returns the count of the givenproperty name for distinctrowscountDistinct("branch")
    • groupProperty Groups the results by the givenpropertygroupProperty("lastName")max Returns the maximum value of thegiven propertymax("age")min Returns the minimum value of thegiven propertymin("age")sum Returns the sum of the givenpropertysum("balance")rowCount Returns count of the number ofrows returnedrowCount()
    •  Criteria can be written as:def query = {and {eq(survey, survey)eq(user,user)}}FormSubmission.createCriteria().list(query)orFormSubmission.createCriteria().list{and {eq(survey, survey)eq(user,user)}}
    •  Adding SQLRestrictions in a criteria: Find all the users with first name, lastnamedob, but their status should not be null ortest_user, and their role should beROLE_USER in userrole table. User UserRole classes are created byspringSecurityPlugin:
    • Role userRole =Role.findByAuthority(ROLE_USER);UserList = User.createCriteria().list{and{if(params.firstName){ilike(firstName,params.firstName+%)}if(params.lastName){ilike(lastName, params.lastName+%)}if(birthDate){eq(birth, birthDate)}sqlRestriction("({alias}.status IS NULLORCAST({alias}.statusAS nvarchar) !=test_user)AND ${userRole.id} in (Selectur.role_id from user_role ur whereur.user_id={alias}.id)")}if (!params.offset) {params.offset = 0}if (!params.max) {params.max = 10}maxResults(params.max.toInteger())firstResult(params.offset.toInteger())}
    •  Support for HibernateQuery Language: find(hql), findAll(hql), and executeQuery(hql) forqueries executeUpdate(hql) for DML‐style opera3ons(sets of updates or deletes) Support for positional and named parametersEAVEntity.executeQuery("SELECT e.id FROM EAVEntity eWHEREe.initialEntryBy=:entryFor AND e.attribute=:attrId",[entryFor:+entryFor+, attrId: this.parentAttribute])
    •  // simple queryAccount.executeQuery("select distinct a.number from Account a") // using with list of parametersAccount.executeQuery("select distinct a.number from Account a " +"where a.branch = ? and a.created > ?",[London, lastMonth]) // using with a single parameter andpagination paramsAccount.executeQuery("select distinct a.number from Account a " +"where a.branch = ?", [London], [max: 10, offset: 5])
    •  // using with a single parameter andpagination paramsAccount.executeQuery("select distinct a.number from Account a " +"where a.branch = ?", [London], [max: 10, offset: 5]) // using with Map of named parametersAccount.executeQuery("select distinct a.number from Account a " +"where a.branch = :branch", [branch: London]) // same as previousAccount.executeQuery("select distinct a.number from Account a "+“where a.branch = :branch", [branch: London], [max: 10, offset: 5])
    •  // tell underlying Hibernate Query object tonot attach newly retrieved // objects to the session, will only save withexplicit saveAccount.executeQuery("select distinct a.number from Account a",null, [readOnly: true]) // time request out after 18 secondsAccount.executeQuery("select distinct a.number from Account a",null, [timeout: 18])
    •  // have Hibernate Query object return 30 rowsat a timeAccount.executeQuery("select distinct a.number from Account a", null,[fetchSize: 30]) // modify the FlushMode of the Query(default is FlushMode.AUTO)Account.executeQuery("select distinct a.number from Account a",null, [flushMode: FlushMode.MANUAL])
    •  The executeQuery method allows theexecution of arbitrary HQL queries. HQL queries can return domain classinstances, or Arrays of specified data whenthe query selects individual fields orcalculated values.
    •  The basic syntax is:Book.executeQuery(String query)Book.executeQuery(String query, List positionalParams)Book.executeQuery(String query, List positionalParams, Map metaParams)Book.executeQuery(String query, Map namedParams)Book.executeQuery(String query, Map namedParams, Map metaParams) query- An HQL query positionalParams - A List of parameters for a positional parameterizedquery namedParams - A Map of named parameters for a namedparameterized query metaParams - A Map of pagination parameters max or/and offset, aswell as Hibernate query parameters readOnly, fetchSize, timeout, and flushMode
    •  Support forWriting native queries: Defining new SQL instance: creating a new sqlinstance, basically it will create a new instance ofthe connection:Sql sql = Sql.newInstance(dataSource) Then we can execute the methods on the sql.sql.rows("SELECT id FROM eavchoice_entity_valueWHEREentity_id=:id",[id:newEntityId]);String entityInsertQuery = "insert into eaventity (version, attribute_id,initial_entry_by)VALUES (0, $attributeId, $entryFor)";def newEntity = sql.executeInsert(entityInsertQuery);sql.close()
    •  Using ExplicitTransaction with Native sqlqueries:▪ // Creating sql instanceSql sql = Sql.newInstance(dataSource)▪ // Instantiating transaction managerDataSourceTransactionManager txm = newDataSourceTransactionManager(dataSource)
    •  // instantiating transaction throughtransaction managerdef status = txm.getTransaction( new DefaultTransactionDefinition() )try{/*Write sql queries*/sql.close()txm.commit(status)}catch(Exception e){log.error("Exception in saving data", e)//Transaction roll backtxm.rollback(status)}finally(){sql.close()//Cleanup resources after transaction completion.txm.cleanupAfterCompletion( status )}
    •  Where Queries: The where method, introduced in Grails 2.0,builds on the support for Detached Criteria byproviding an enhanced, compile-time checkedquery DSL for common queries. The where method is more flexible than dynamicfinders, less verbose than criteria and provides apowerful mechanism to compose queries.
    •  Basic Querying: The where method accepts a closure that looksvery similar to Groovys regular collectionmethods. The closure should define the logical criteria inregular Groovy syntax, for example:def query = Person.where {firstName == "Bart"}Person bart = query.find()
    •  The returned object is a DetachedCriteriainstance, which means it is not associated withany particular database connection or session. This means you can use the where method todefine common queries at the class level:class Person {static simpsons = where {lastName == "Simpson"}…}
    •  Query execution is lazy and only happens uponusage of the DetachedCriteria instance. If you want to execute a where-style queryimmediately there are variations of the findAlland find methods to accomplish this:def results = Person.findAll {lastName == "Simpson"}def results = Person.findAll(sort:"firstName") {lastName == "Simpson"}Person p = Person.find { firstName == "Bart" }
    •  Each Groovy operator maps onto a regular criteria method. The following table provides a map of Groovy operators tomethods:Operator Criteria Method Description== eq Equal to!= ne Not Equal to> gt Greater than< lt Less than>= ge Greater than or equal to<= le Less than or equal toin inList Contained within the given list==~ like Like a given string=~ ilike Case insensitive like
    •  It is possible use regular Groovy comparisonoperators and logic to formulate complex queries:def query = Person.where {(lastName != "Simpson" && firstName != "Fred") || (firstName == "Bart" &&age > 9)}def results = query.list(sort:"firstName") The Groovy regex matching operators map onto likeand ilike queries unless the expression on the right handside is a Pattern object, in which case they map onto anrlike query:def query = Person.where {firstName ==~ ~/B.+/} ** Note that rlike queries are only supported if the underlying database supports regularexpressions
    •  A between criteria query can be done by combiningthe in keyword with a range:def query = Person.where {age in 18..65} Finally, you can do isNull and isNotNull style queriesby using null with regular comparison operators:def query = Person.where {middleName == null}
    •  Query Composition: Since the return value of the where method is aDetachedCriteria instance you can compose newqueries from the original query:def query = Person.where {lastName == "Simpson"}def bartQuery = query.where {firstName == "Bart"}Person p = bartQuery.find()
    •  Note that you cannot pass a closure defined as avariable into the where method unless it has beenexplicitly cast to a DetachedCriteria instance. In other words the following will produce an error:def callable = {lastName == "Simpson"}def query = Person.where(callable)
    •  The above must be written as follows:import grails.gorm.DetachedCriteriadef callable = {lastName == "Simpson"} as DetachedCriteria<Person>def query = Person.where(callable) As you can see the closure definition is cast (usingthe Groovy as keyword) to a DetachedCriteriainstance targeted at the Person class.
    •  Conjunction, Disjunction and Negation: As mentioned previously you can combine regularGroovy logical operators (|| and &&) to formconjunctions and disjunctions:def query = Person.where {(lastName != "Simpson" && firstName != "Fred") || (firstName == "Bart" &&age > 9)} You can also negate a logical comparison using !:def query= Person.where {firstName == "Fred" && !(lastName == Simpson)}
    •  Property Comparison Queries: If you use a property name on both the left handand right side of a comparison expression thenthe appropriate property comparison criteria isautomatically used:def query = Person.where {firstName == lastName}
    •  The following table described how each comparisonoperator maps onto each criteria propertycomparison method:Operator Criteria Method Description== eqProperty Equal to!= neProperty Not equal to> gtProperty Greater than< ltProperty Less than>= geProperty Greater than or equalto<= leProperty Less than or equal to
    •  Querying Associations: Associations can be queried by using the dotoperator to specify the property name of theassociation to be queried:def query = Pet.where {owner.firstName == "Joe" || owner.firstName == "Fred"}
    •  You can group multiple criterion inside a closuremethod call where the name of the methodmatches the association name:def query = Person.where {pets { name == "Jack" || name == "Joe" }} This technique can be combined with other top-level criteria:def query = Person.where {pets { name == "Jack" } || firstName == "Ed"}
    •  For collection associations it is possible toapply queries to the size of the collection:def query = Person.where {pets.size() == 2}
    •  Subqueries: It is possible to execute subqueries within wherequeries. For example to find all the people olderthan the average age the following query can beused:final query = Person.where {age > avg(age)}
    •  The following table lists the possiblesubqueries:Method Descriptionavg The average of all valuessum The sum of all valuesmax The maximum valuemin The minimum valuecount The count of all valuesproperty Retrieves a property of the resultingentities
    •  You can apply additional criteria to any subquery byusing the of method and passing in a closure containingthe criteria:def query = Person.where {age > avg(age).of { lastName == "Simpson" } && firstName == "Homer"} Since the property subquery returns multiple results,the criterion used compares all results. For example the following query will find all peopleyounger than people with the surname "Simpson":Person.where {age < property(age).of { lastName == "Simpson" }}
    •  There are several functions available to you within thecontext of a query.These are summarized in the table below:Method Descriptionsecond The second of a date propertyminute The minute of a date propertyhour The hour of a date propertyday The day of the month of a date propertymonth The month of a date propertyyear The year of a date propertyupper Converts a string property to upper caselower Converts a string property to lower caselength The length of a string propertytrim Trims a string property
    •  **Currently functions can only be applied to propertiesor associations of domain classes.You cannot, forexample, use a function on a result of a subquery. For example the following query can be used to findall pets born in 2011:def query = Pet.where {year(birthDate) == 2011} You can also apply functions to associations:def query = Person.where {year(pets.birthDate) == 2009}
    •  Batch Updates and Deletes:▪ Since each where method call returns aDetachedCriteria instance, you can use where queries toexecute batch operations such as batch updates anddeletes.▪ For example, the following query will update all peoplewith the surname "Simpson" to have the surname"Bloggs":def query = Person.where {lastName == Simpson}int total = query.updateAll(lastName:"Bloggs")
    •  **Note that one limitation with regards to batchoperations is that join queries (queries that queryassociations) are not allowed. To batch delete records you can use the deleteAllmethod:def query = Person.where {lastName == Simpson}int total = query.deleteAll()