NEO

蜀道难,难于上青天!

导航

Grails指南摘要-302-瞬时属性

Posted on 2013-06-04 21:20  页面载入出错  阅读(199)  评论(0编辑  收藏  举报

domain对象

class Company {
    String name
    Integer numberOfEmployees 
    BigDecimal salaryPaidYTD
    static transients = ['salaryPaidYTD']
}

salaryPaidYTD属性为瞬时属性,内容不会被保存到数据库

还可以用get***方式处理瞬时属性,如下

class Company {
    BigDecimal cash
    BigDecimal receivables
    BigDecimal capital
    BigDecimal getNetWorth() {
        cash + receivables + capital
    }
    static transients = ['netWorth']
}

此时的netWorth属性声明为瞬时属性

还可以用派生属性的方式声明瞬时属性,如下

class Company {
    BigDecimal cash
    BigDecimal receivables
    BigDecimal capital
    BigDecimal netWorth
    static mapping = {
        netWorth formula: 'CASH + RECEIVABLES + CAPITAL'
    }
}