grails源属性在数据库中创建列

时间:2014-12-05 18:52:41

标签: grails gorm

我使用的是grails 2.4.4。

我有域名

class Post {
    Integer nbrOfFavorites
    static hasMany = [
        favorites : Favorite
    ]

    static mappings = {
        nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))'
    }
}

问题是nbrOfFavorites正在数据库中创建,因此检索它并没有考虑公式。

我的语法有问题吗?

由于

2 个答案:

答案 0 :(得分:3)

是的,语法中有拼写错误。将mappings更改为mapping,例如:

static mapping = {
    nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))'
}

参考#Grails Domain: mapping

答案 1 :(得分:0)

尝试将此字段作为瞬态添加到域对象:

class Post {
Integer nbrOfFavorites
static hasMany = [
    favorites : Favorite
]

static mappings = {
    nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))'
}

static transients = ['nbrOfFavorites']
}