【问题标题】:Index a foreign key in Grails / Gorm在 Grails / Gorm 中索引外键
【发布时间】:2014-01-20 12:45:32
【问题描述】:

使用 Grails / Gorm,我可以通过以下方式定义索引:

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

但是,如果我使用如下连接表:

class Employee {
    static hasMany = [projects: Project]

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

如何配置它以使连接表中的列被索引?

谢谢

【问题讨论】:

    标签: hibernate grails grails-orm


    【解决方案1】:

    如果您使用的是database migration plugin,您可以使用createIndex 更改集来创建索引:

    changeSet(author: "..", id: "..") {
      createIndex(indexName: "indexname", tableName: "yourtable", unique: "true") {
        column(name: "country_code")
      }
    }
    

    【讨论】:

      【解决方案2】:

      在 Grails 中似乎没有任何 DSL 可以这样做。但是,您始终可以在 hibernate configuration 中为这些连接表设置索引。以下是上述配置文件的示例。

      <?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> 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-18
        • 1970-01-01
        • 1970-01-01
        • 2011-10-04
        • 1970-01-01
        相关资源
        最近更新 更多