How can I change column name convention in Grails?
Asked Answered
P

2

5

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

Pedant answered 14/9, 2010 at 13:37 Comment(0)
A
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
}
Amylum answered 14/9, 2010 at 13:49 Comment(6)
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.MyCoolNamingStrategyLe
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. docsBrooks
I raised this as a bug, you can follow it here jira.grails.org/browse/GRAILS-11988Salian
Working documentation URL: grails.github.io/grails-doc/2.4.4/guide/…Laevo
Grails 3.0.1 - Take a look at this bug github.com/grails/grails-core/issues/667 could help a lotUnpaged
This is a way over-complicated answer. It is much easier to just use the static mapping = {} shown by Aaron Saunders answer.Jeffries
F
8

5.5.2.1 Table and Column Names

class Person {
  String firstName
  static mapping = {
      table 'people'
      firstName column:'firstname'
  }
}
Finding answered 14/9, 2010 at 13:41 Comment(1)
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.SchemaUpdateRaja
A
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
}
Amylum answered 14/9, 2010 at 13:49 Comment(6)
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.MyCoolNamingStrategyLe
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. docsBrooks
I raised this as a bug, you can follow it here jira.grails.org/browse/GRAILS-11988Salian
Working documentation URL: grails.github.io/grails-doc/2.4.4/guide/…Laevo
Grails 3.0.1 - Take a look at this bug github.com/grails/grails-core/issues/667 could help a lotUnpaged
This is a way over-complicated answer. It is much easier to just use the static mapping = {} shown by Aaron Saunders answer.Jeffries

© 2022 - 2024 — McMap. All rights reserved.