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

More Information:

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 leave these two fields as-is:

Protected by Invisible Defender. Showed 403 to 81,013 bad guys.

Search
Categories