Search code examples
hibernategrailsgrails-orm

Index a foreign key in Grails / Gorm


Using Grails / Gorm, I can define indexes by doing something like:

class Person {
    String firstName
    static mapping = {
        table 'people'
        id column: 'person_id'
        firstName column: 'First_Name', index: 'Name_Idx'
    }
}

However, if I am using a join table as in:

class Employee {
    static hasMany = [projects: Project]

    static mapping = {
        projects joinTable: [name: 'EMP_PROJ',
                             column: 'PROJECT_ID',
                             key: 'EMPLOYEE_ID']
    }
}

How do I configure it so that the columns in the join table are indexed?

Thanks


Solution

  • It would appear that there isn't any DSL within Grails to do so. However, you can always setup a index in your hibernate configuration for these join tables. Here is an example of said configuration file.

    <?xml version='1.0' encoding='UTF-8'?> 
    
    <!DOCTYPE hibernate-mapping PUBLIC 
       '-//Hibernate/Hibernate Mapping DTD 3.0//EN' 
       'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'> 
    
    <hibernate-mapping> 
       <database-object> 
          <create>CREATE INDEX foo_idx ON bar (some_id, other_column)</create> 
          <drop/> 
          <dialect-scope name='org.hibernate.dialect.MySQL5InnoDBDialect' /> 
       </database-object> 
    </hibernate-mapping>