grails - 如何在 Grails 2.2 中设置域类值的默认值?

标签 grails grails-2.0 grails-domain-class

在我的 Grails 域类中,我想设置在数据库中保留的默认值。我使用mysql作为数据库。我试图这样做:

class A {

   long someValue = 1
   long someOtherValue
   boolean someBool = true
   boolean someOtherBool

   static mapping = {
      someOtherValue defaultValue: 1
      someOtherBool defaultValue: true  
   }
}

但没有任何作用。数据库中没有设置默认值。我必须更改什么才能正确设置我的默认值?

最佳答案

如果你在 Grails 2.2 以上,那么你可以使用 defaultValue。看看伯特的回答 here
试试看,希望这有帮助:

Class A {
      Long someValue 
      Long someOtherValue

      Boolean someBool
      Boolean someOtherBool

     static mapping = {
        someOtherValue defaultValue: 1
        someOtherBool  defaultValue: true  
        ...
     } 

}

关于grails - 如何在 Grails 2.2 中设置域类值的默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16505968/

相关文章:

hibernate - Grails域映射异常

grails - Grails-在不更改URL的情况下检索“注册”页面或主页

grails - 域类保存和 session 保存有什么区别

grails - grails控制列表方法的外观

maven - 如何在 Grails 2.4 中外部化 Maven 凭据

grails - Grails 2.0中的dateCreated,lastUpdated字段

grails - Grails 1.4与2.0

grails - 在 Grails Controller 中对父子进行排序

grails - 在 Grails 中将 xml 转换为域对象

jsp - Grails - 使用自定义 JSP 标签库