Last updated by 5 years ago

Page: GORM explicit mapping definitions, Version:1

GORM explicit mapping definitions

Explicit mapping is only necessary where DB schema is not compliant with GORM conventions. It is used to override conventions.

Existing DB schema column names (not compliant with GORM naming conventions) and Hibernate type mapping definition

static propertyMappings = {
	firstName(column:'name_first')                       // column name not compliant, Hibernate type ok
	dob(type:'date')                                     // column name ok, Hibernate type declared
 	creationDate(column:'create_dt', type:'timestamp')   // both column name and Hibernate type specified

Turn off versioning when not supported by existing DB schema

static versioning = false

id name/generation strategy definition for existing DB schema

static id = {

static id = { idMapping(name:'personId',column:'uid',unsavedValue:0) generator(class:'hilo',table:'hi_value',column:'next',maxLo:100) } or

static id = { idMapping(name:'personId',column:'uid',unsavedValue:0) generator(class:'identity') }

Database index definitions

We need a way withing GORM and other possible ORMs to define the indexes to be created on the database, to aid in performance optimisation.


  • Allow performance tuning for queries (unlikely to affect normal object graph mechanisms, may negatively impact performance of updates if indexes defined - but should be good for HQL/Criteria queries and depending on DB, optimising projections i.e. order by?)
  • Support creation of indexes on any column of a domain class
  • Support creation of indexes on any combination of columns of a domain class
Please feel free to add variants below, with a new option number.

Option 1

Indexes have names and can span properties (columns) so this option uses a new "indexes" static property closure on domain classes rather than the existing constraints mechanism:

class Book {
    String title
    Author author
    String ISBN
    Date publishedDate

static constraints = { ISBN(blank:false) title(size:1..100) }

static indexes = { isbn('ISBN') // single column ISBN index authorTitle('author', 'title') // two-column index publishedDate() // Implicit publishedDate index, no need to specify name again } }

We have to ensure what we want to do is achievable with hibernate. It doesn't appear to support ASC/DESC indexes.

Also it is not clear whether or not we could generate an index that contains property values from an object in the relationship. This could possibly be a big performance win if it is supported by hibernate.

With optional parens in Groovy this example can reduce to:

static indexes = {
        isbn 'ISBN' // single column ISBN index
        authorTitle 'author', 'title'  // two-column index
        publishedDate() // Implicit publishedDate index, no need to specify name again

Group all mapping related properties

static mapping = {

withTable 'great_legacy_table_name' versioning false propertyMappings { firstName(column:'name_first') lastName(column:'name_last') dob(type:'date') creationDate(column:'create_dt', type:'date') }

id { idMapping(name:'personId',column:'uid',unsavedValue:0) generator(class:'sequence',name:'person_seq') }

indexes { isbn 'ISBN' authorTitle 'author', 'title' publishedDate() } }