New GORM Features Coming in 1.1
Posted by:
Graeme Rocher
on 10/31/2008
Now that The Definitive Guide to Grails 2nd Edition (barring a few reviews) is a wrap, I've been refocusing in Grails development. First up, is GORM and we've implemented a number of great improvements including:
Better GORM events
Previously, GORM supported beforeInsert, beforeUpdate and beforeDelete events, now there is afterInsert, afterUpdate and afterDelete to complete the picture
Persistence of Collections of Basic Types
GORM now supports persisting basic types like String, Integer and so on using a join table:
It is now simpler to bind data to a subset of properties. Previously you could use the syntax:
Persistent instances can now be loaded in a read-only state using the read method:
Associations can now be sorted using a default sort order declared at the class level:
GORM now supports configuring batch fetching (an optimization of lazy loading) using the ORM DSL at the class level:
There is a new InList suffix that can be used with dynamic finders:
Many-to-many and Unidirectional One-to-many associations can use the joinTable argument to alter the way they map to the underlying database:
Better GORM events
Previously, GORM supported beforeInsert, beforeUpdate and beforeDelete events, now there is afterInsert, afterUpdate and afterDelete to complete the picture
Persistence of Collections of Basic Types
GORM now supports persisting basic types like String, Integer and so on using a join table:
class Person {
static hasMany = [nicknames:String]
}Improvements to Data BindingIt is now simpler to bind data to a subset of properties. Previously you could use the syntax:
person.properties = paramsWhich would bind all the incoming request parameters to the person. If you didn't want that behavior you could use the bindData method. Now you can bind to a subset of properties using the subscript operator:person.properties["firstName","lastName"] = paramsAnd access a subset of the domain classes properties using the same syntax:person.properties["firstName","lastName"].each { println it }Read-Only Access to ObjectsPersistent instances can now be loaded in a read-only state using the read method:
def book = Book.read(1)Default Sort OrderAssociations can now be sorted using a default sort order declared at the class level:
class Book {
String title
static mapping = {
sort "title"
}
}Or at the association level:class Author {
static hasMany = [books:Book]
static mapping = {
books sort:"title"
}
}Batch FetchingGORM now supports configuring batch fetching (an optimization of lazy loading) using the ORM DSL at the class level:
class Book {
String title
static mapping = {
batchSize 15
}
}Or at the association level:class Author {
static hasMany = [books:Book]
static mapping = {
books batchSize:15
}
}Improvements to Dynamic FindersThere is a new InList suffix that can be used with dynamic finders:
def groovyBooks = Book.findByAuthorInList(['Dierk Koenig', 'Graeme Rocher'])Dynamic finders can also now use the query cache:def books = Book.findByTitle("Groovy in Action", [cache:true] )And upgrade to a pessimistic lock:def books = Book.findByTitle("Groovy in Action", [lock:true] )Legacy Mapping for Many-to-Many and Unidirectional One-to-manysMany-to-many and Unidirectional One-to-many associations can use the joinTable argument to alter the way they map to the underlying database:
class Book {
String title
static belongsTo = Author
static hasMany = [authors:Author]
static mapping = {
authors joinTable:[name:"mm_author_books", key:'mm_book_id' ]
}
}
class Author {
String name
static hasMany = [books:Book]
static mapping = {
books joinTable:[name:"mm_author_books", key:'mm_author_id']
}
}
Graeme Rocher's complete blog can be found at: http://graemerocher.blogspot.com
About Graeme Rocher
As Head of Grails Development for SpringSource, Graeme Rocher is the project lead and co-founder of the Grails web application framework. He's a member of the JSR-241 Expert Group which standardizes the Groovy language. Graeme authored the Definitive Guide to Grails for Apress and is a frequent speaker at JavaOne, JavaPolis, NoFluffJustStuff, JAOO, the Sun TechDays and more. Graeme joined SpringSource in late 2008 upon the acquisition of G2One Inc. Before founding G2One, Graeme was the CTO of SkillsMatter, a skills transfer company specializing in open source technology and agile software development, where Graeme was in charge of the company's courseware development strategy and general technical direction.
More About Graeme »NFJS, the Magazine
December Issue Now AvailableBDD and REST
by Brian SlettenMocks and Stubs in Groovy Tests
by Kenneth KousenAlgorithms for Better Text Search Results
by John GriffinKnowns and Unknowns of Scrum and Agile
by Brian Tarbox