grails的domain的constraints和mapping

constraints:

name nullable:true,maxSize:5     //字段允許爲空,如果插入數據,maxSize爲5

其餘查看官方文檔


mapping:

txtContent sqlType: 'text'

autoImport
數據庫表名是唯一的。
如果不同包中,domain類的名字一樣,會導致org.hibernate.DuplicateMappingException. 解決辦法:

static mapping = {
    autoImport false
}

查看數據庫:兩個表的字段會合併到一個表中。
By default the domain classes are auto-imported in HQL queries so you aren’t required to specify the whole class name including the package. Remember that you need to use the fully qualified class name in the HQL query for references to these classes.
如何插入數據呢?

autoTimestamp

Usage: autoTimestamp(boolean)

By default when you have properties called dateCreated and/or lastUpdated in a domain class, Grails automatically maintains their state in the database. You can disable this by setting autoTimestamp to false:

static mapping = {
autoTimestamp false
}

static mapping = {
autoImport false
}

batchSize
Given a lazy association where an Author has many Books, GORM will perform one query for the Author and additional queries the associated Books. This is what is known as the N+1 problem and can often be worked around by using a join query. However, joins can be expensive.
Batch fetching is an optimization of lazy loading so that if, for example, you set a batchSize of 10 and you have an Author that with 30 books, instead of 31 queries you get four (one for the Author and three batches of 10):

static mapping = {
    batchSize 10
}

You can also configure batchSize on a per association basis:

class Author {
    static hasMany = [books: Book]
    static mapping = {
        books batchSize: 10
    }
}

cache
Usage: cache(boolean/string/map)
Arguments:

* usage - The cache usage. Can be read-only, read-write, nonstrict-read-write or transactional
* include (optional) - Whether to include non-lazy associations. Can be all or non-lazy

You enable caching per domain class, for example:

static mapping = {
    cache true
}

This will configure the domain class to use a ‘read-write’ cache, but you can configure whatever cache policy is appropriate (and supported by the cache implementation):

static mapping = {
    cache 'transactional'
}

or

static mapping = {
    cache usage: 'read-only', include: 'non-lazy'
}

You can also configure the cache policy on a per-association basis:

class Author {
    static hasMany = [books: Book]
    static mapping = {
        books cache: true // or 'read-write' etc.
    }
}

其餘查看官方文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章