(Quick Reference)

1.1.4 持久层特性 - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

1.1.4 持久层特性

The GORM API

The GORM API has been formalized into a set of classes (GormStaticApi, GormInstanceApi and GormValidationApi) that get statically wired into every domain class at the byte code level. The result is better code completion for IDEs, better integration with Java and the potential for more GORM implementations for other types of data stores.

GORM API

GORM API现在已正式规范为类的集合(GormStaticApi, GormInstanceApi, GormValidationApi),并且在每个领域类的字节码级别上进行注入的。如此一来,对IDE的代码补齐,Java的集成,以及潜在的其他类型的GORM实现来说,提供了更好的支持。

New findOrCreate and findOrSave Methods

Domain classes have support for the findOrCreateWhere, findOrSaveWhere, findOrCreateBy and findOrSaveBy query methods which behave just like findWhere and findBy methods except that they should never return null. If a matching instance cannot be found in the database then a new instance is created, populated with values represented in the query parameters and returned. In the case of findOrSaveWhere and findOrSaveBy, the instance is saved before being returned.

def book = Book.findOrCreateWhere(author: 'Douglas Adams', title: "The Hitchiker's Guide To The Galaxy")
def book = Book.findOrSaveWhere(author: 'Daniel Suarez', title: 'Daemon')
def book = Book.findOrCreateByAuthorAndTitle('Daniel Suarez', 'Daemon')
def book = Book.findOrSaveByAuthorAndTitle('Daniel Suarez', 'Daemon')

全新的findOrCreate和findOrSave方法

领域类现在已经支持findOrCreateWhere, findOrSaveWhere, findOrCreateBy和findOrSaveBy查询方法,这些方法除了不返回null值以外,跟findWhere和findBy方法基本类似。如果在数据库中没有找到符合条件的实例,系统将会根据查询参数创建一个全新的实例返回,不过在findOrSaveWhere 和findOrSaveBy中,实例是先保存入库再返回的。示例代码如下:

def book = Book.findOrCreateWhere(author: 'Douglas Adams', title: "The Hitchiker's Guide To The Galaxy")
def book = Book.findOrSaveWhere(author: 'Daniel Suarez', title: 'Daemon')
def book = Book.findOrCreateByAuthorAndTitle('Daniel Suarez', 'Daemon')
def book = Book.findOrSaveByAuthorAndTitle('Daniel Suarez', 'Daemon')

Detached Criteria and Where Queries

Grails 2.0 features support for DetachedCriteria which are criteria queries that are not associated with any session or connection and thus can be more easily reused and composed:

def criteria = new DetachedCriteria(Person).build {
    eq 'lastName', 'Simpson'
}
def results = criteria.list(max:4, sort:"firstName")

To support the addition of DetachedCriteria queries and encourage their use a new where method and DSL has been introduced to greatly reduce the complexity of criteria queries:

def query = Person.where {
    (lastName != "Simpson" && firstName != "Fred") || (firstName == "Bart" && age > 9)
}
def results = query.list(sort:"firstName")

See the documentation on DetachedCriteria and Where Queries for more information.

分离的Criteria和Where查询

Grails 2.0中分离的Criteria是指条件(Criteria)查询不再跟任何数据库会话或者连接关联,因此可以很方便的复用和构造,比如:

def criteria = new DetachedCriteria(Person).build {
    eq 'lastName', 'Simpson'
}
def results = criteria.list(max:4, sort:"firstName")

为了支持分离的Criteria查询和减少条件查询的复杂性,一个新的where方法及其DSL被引入,比如:

def query = Person.where {
    (lastName != "Simpson" && firstName != "Fred") || (firstName == "Bart" && age > 9)
}
def results = query.list(sort:"firstName")

更多信息请参考本手册的分离的Criteria and Where Queries章节。

Abstract Inheritance

GORM now supports abstract inheritance trees which means you can define queries and associations linking to abstract classes:

abstract class Media {
    String title
    …
}
class Book extends Media {
}
class Album extends Media {

} class Account { static hasMany = [purchasedMedia:Media] }

..

def allMedia = Media.list()

抽象继承

GORM现在支持抽象的继承树或者说现在你可以定义抽象类的查询和关联了,比如:

abstract class Media {
    String title
    …
}
class Book extends Media {
}
class Album extends Media {

} class Account { static hasMany = [purchasedMedia:Media] }

..

def allMedia = Media.list()

Multiple Data Sources Support

It is now possible to define multiple datasources in DataSource.groovy and declare one or more datasources a particular domain uses by default:

class ZipCode {

String code

static mapping = { datasource 'ZIP_CODES' } }

If multiple datasources are specified for a domain then you can use the name of a particular datasource as a namespace in front of any regular GORM method:

def zipCode = ZipCode.auditing.get(42)

For more information see the section on Multiple Data Sources in the user guide.

支持多个数据源

现在,可以在DataSource.groovy定义多个数据源了,对于特定的领域类来说,声明一个或者多个数据源的示例如下:

class ZipCode {

String code

static mapping = { datasource 'ZIP_CODES' } }

在使用有多个数据源的领域类时候,只需要在常规的GORM方法前加上特定数据源名称即可。比如:

def zipCode = ZipCode.auditing.get(42)

更多信息请参考本手册的多数据源章节

Database Migrations

A new database migration plugin has been designed and built for Grails 2.0 allowing you to apply migrations to your database, rollback changes and diff your domain model with the current state of the database.

数据库迁移(Database Migration)

新设计的数据库迁移插件完全是基于Grails 2.0的,有了它,你可以迁移你的数据库了,比如根据当前的数据库状态回滚所做的变化,比较领域模型。

Database Reverse Engineering

A new database reverse engineering plugin has been designed and built for Grails 2.0 that allows you to generate a domain model from an existing database schema.

数据库逆向工程

基于Grails 2.0的数据库逆向工程插件可以根据已有的数据库模式(database schema),自动生成领域模型。

Hibernate 3.6

Grails 2.0 is now built on Hibernate 3.6

Hibernate 3.6

Grails 2.0现在基于Hibernate 3.6了

Bag Collections

You can now use Hibernate Bags for mapped collections to avoid the memory and performance issues of loading large collections to enforce Set uniqueness or List order.

For more information see the section on Sets, Lists and Maps in the user guide.

Bag集合

现在Hibernate的Bag集合(Bag集合,即在集合中允许重复,可以简单的看作为Set和List的结合体--译者注)在Grails 2.0中也得到了支持,由此也比较好的解决了加载大数据量集合转换(Set必须唯一或者List必须有序)导致的内存和性能问题。

更多信息请参考本手册的集合、列表、映射章节。