在我的Grails域类中,我想设置在数据库中保留的默认值。我使用mysql作为数据库。我试着这样做:
class A {
long someValue = 1
long someOtherValue
boolean someBool = true
boolean someOtherBool
static mapping = {
someOtherValue defaultValue: 1
someOtherBool defaultValue: true
}
}
但没有任何作用。数据库中没有设置默认值。我需要更改什么才能正确设置默认值?
答案 0 :(得分:6)
如果您使用的是上面的Grails 2.2,那么您可以使用defaultValue。看看伯特的回答here 试试吧,希望这会有所帮助:
Class A {
Long someValue
Long someOtherValue
Boolean someBool
Boolean someOtherBool
static mapping = {
someOtherValue defaultValue: 1
someOtherBool defaultValue: true
...
}
}
答案 1 :(得分:2)
我发现defaultValue使用String属性,我需要在单引号周围加双引号,defaultValue用于数字属性,我需要在数字周围放置双引号,否则默认值不会出现在DDL。所以,例如:
static mapping = {
myStringProperty defaultValue: "'Cash'"
myIntProperty defaultValue: "0"
}
另外,据我所知,默认值不适用于枚举属性。
答案 2 :(得分:2)
class A {
long someValue
long someOtherValue
boolean someBool = Boolean.TRUE
boolean someOtherBool = Boolean.TRUE
static mapping = {
someValue defaultValue: '1'
someOtherValue defaultValue: '1'
}
}
这将在2.2.3中测试。