Skip to content

Instantly share code, notes, and snippets.

@gschueler
Created April 24, 2017 22:58
Show Gist options
  • Save gschueler/25bbde81c5985401b9d5effb4253f4c5 to your computer and use it in GitHub Desktop.
Save gschueler/25bbde81c5985401b9d5effb4253f4c5 to your computer and use it in GitHub Desktop.
Grails indexes (2.x)

Notes on adding index definitions to Grails 2 domain classes

Add an index named MY_INDEX to two columns:

Inside your Domain class:

static mapping={
    field1 index:'MY_INDEX'
    field2 index:'MY_INDEX'
}

that is simple enough. It gets ugly if you want to create multiple indexes for multiple different columns.

See the DomainIndexHelper.groovy

Subclasses

If you have a A > B hierarchy of domain classes, then the fields in A are apparently not visible in the mapping section of B

class A{ 
   String field1
}
class B extends A{
    String field2
    static mapping={
        field1 index:'MY_INDEX' //doesn't work
        field2 index:'MY_INDEX'
    }
}

The solution is to define the index name for the fields in A mapping:

class A{ 
   String field1
   
    static mapping={
        field1 index:'MY_INDEX' 
    }
}
class B extends A{
    String field2
    static mapping={
        field2 index:'MY_INDEX'
    }
}

Table per hierarchy domain with a class field

Grails implicitly adds a class field to the table, if you are using "table-per-hierarchy". This means that basically every query for that table includes a where class = 'B' clause, but unfortunately, it appears that implicit field is not available to index defintions within the mapping clause :(((.

See: issue#6815

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment