For now I have field "String firstName" it converted to "first_name" and i want "firstname" as default in Hibernate. Is it posible?
2 Answers
5.5.2.1 Table and Column Names
class Person {
String firstName
static mapping = {
table 'people'
firstName column:'firstname'
}
}

- 33,180
- 5
- 60
- 80
-
This perfectly solved my problem where it was a single class name causing conflicts with MySQL (I had a class named 'condition' which led to improper syntax in hbm2ddl.SchemaUpdate – Mike May 03 '12 at 20:49
-
Simple answer. Thanks! – Alex Beardsley Dec 16 '13 at 20:53
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
}
-
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
-
1Big 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