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 = params
Which 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"] = params
And 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']
}
}