Named Queries in Grails 1.2

April 6th, 2010 by

They built a nice new feature in Grails 1.2 called “named queries“. Named queries can be defined in a domain class as static properties and support the criteria builder syntax.

 

Examples

package testapp
 
class User {
	String name
	int iq
	int age
 
	static namedQueries = {
		dumbUsers {
			int referenceIq = 60
			lt 'iq' , referenceIq
		}
 
		nameStartsWith { letter ->
			like 'name', '${letter}%'
		}
 
		midAges {
			between('age', 20, 40)
		}
 
	}
}
 
// count dumb users
println User.dumbUsers.count()
 
// print amount of users, usernames starting with an 'a'
User.nameStartsWith('a').count()

Resources

Article Updates

  • 2015-03-03: Table of contents added.
    package testapp

    class User {
    String name
    int iq

    static namedQueries = {
    dumbUsers {
    int referenceIq = 60
    lt ‘iq’ , referenceIq
    }

    nameStartsWith { letter ->
    like ‘name’, ‘${letter}%’
    }

    }
    }

    Tags: , , , , , ,

    2 Responses to “Named Queries in Grails 1.2”

    1. fransu7 Says:

      1.2 ;) check your title.

    2. micha kops Says:

      thanks! :)

    Leave a Reply

    Please note, that no personal information like your IP address is stored and you're not required to enter you real name.

    Comments must be approved before they are published, so please be patient after having posted a comment - it might take a short while.

    Please leave these two fields as-is:
    Search
    Categories