5

For now I have field "String firstName" it converted to "first_name" and i want "firstname" as default in Hibernate. Is it posible?

Steve K
  • 19,408
  • 6
  • 52
  • 50
yura
  • 14,489
  • 21
  • 77
  • 126

2 Answers2

8

5.5.2.1 Table and Column Names

class Person {
  String firstName
  static mapping = {
      table 'people'
      firstName column:'firstname'
  }
}
Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80
6

You can change the naming strategy for the entire project. From the documentation https://grails.github.io/grails-doc/latest/guide/GORM.html#customNamingStrategy.

By default Grails uses Hibernate's ImprovedNamingStrategy to convert domain class Class and field names to SQL table and column names by converting from camel-cased Strings to ones that use underscores as word separators. You can customize these on a per-instance basis in the mapping closure but if there's a consistent pattern you can specify a different NamingStrategy class to use.

Configure the class name to be used in grails-app/conf/DataSource.groovy in the hibernate section, e.g.

So, something like this in your DataSource.groovy

dataSource {
    pooled = true
    dbCreate = "create-drop"
     …
}
hibernate {
    cache.use_second_level_cache = true
     …
    naming_strategy = org.hibernate.cfg.DefaultNamingStrategy
}
Cristan
  • 12,083
  • 7
  • 65
  • 69
Steve K
  • 19,408
  • 6
  • 52
  • 50
  • You can also implement org.hibernate.cfg.NamingStrategy and use that if one of the implementations from Hibernate isn't sufficient. Just put it in src/groovy or src/java and reference it as `naming_strategy = com.myco.myapp.MyCoolNamingStrategy` – Burt Beckwith Sep 14 '10 at 15:05
  • 1
    Big caveat in the current state of this feature in Grails: this naming strategy override currently isn't applied for joins, foreign keys, or embedded objects. Underscores are hardcoded in the default mapping for those. So if you want to change the naming strategy, you'll need to specify column name overrides for all of those properties in your domain objects. [docs](http://grails.org/doc/latest/guide/GORM.html#tableAndColumnNames) – Dan Tanner Aug 10 '12 at 15:44
  • I raised this as a bug, you can follow it here https://jira.grails.org/browse/GRAILS-11988 – Doron Manor Feb 16 '15 at 03:24
  • Working documentation URL: http://grails.github.io/grails-doc/2.4.4/guide/GORM.html#customNamingStrategy – krzychu Mar 17 '15 at 21:37
  • Grails 3.0.1 - Take a look at this bug https://github.com/grails/grails-core/issues/667 could help a lot – Guillaume Besse May 20 '15 at 17:00
  • This is a way over-complicated answer. It is much easier to just use the static mapping = {} shown by Aaron Saunders answer. – Michael Aug 07 '15 at 14:10