我使用的是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正在数据库中创建,因此检索它并没有考虑公式。
我的语法有问题吗?
由于
答案 0 :(得分:3)
是的,语法中有拼写错误。将mappings
更改为mapping
,例如:
static mapping = {
nbrOfFavorites formula: '(select count(1) from favorite f where (f.post_id = id))'
}
答案 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']
}