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.
    }
}

其余查看官方文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值