5

现在我将字段“String firstName”转换为“first_name”,并且我希望“firstname”作为 Hibernate 中的默认值。有可能吗?

4

2 回答 2

8

5.5.2.1 表名和列名

class Person {
  String firstName
  static mapping = {
      table 'people'
      firstName column:'firstname'
  }
}
于 2010-09-14T13:41:56.167 回答
6

您可以更改整个项目的命名策略。从文档https://grails.github.io/grails-doc/latest/guide/GORM.html#customNamingStrategy

默认情况下,Grails 使用 Hibernate 的改进命名策略,通过将驼峰式字符串转换为使用下划线作为单词分隔符的字符串,将域类类和字段名称转换为 SQL 表和列名称。您可以在映射闭包中基于每个实例自定义这些,但如果存在一致的模式,您可以指定要使用的不同 NamingStrategy 类。

在 hibernate 部分的 grails-app/conf/DataSource.groovy 中配置要使用的类名,例如

所以,在你的 DataSource.groovy 中是这样的

dataSource {
    pooled = true
    dbCreate = "create-drop"
     …
}
hibernate {
    cache.use_second_level_cache = true
     …
    naming_strategy = org.hibernate.cfg.DefaultNamingStrategy
}
于 2010-09-14T13:49:49.897 回答