2016-12-02 107 views
0
class Tooth { 
    Integer id 
    ToothDisease toothDisease 
    static mapping = { 
    table name: 'AK_TOOTH' 
    id generator: 'sequence', params: [sequence: 'SEQ_AK_TOOTH'] 
    columns{ 
     toothDisease { 
     column: 'FK_AK_TOOTH_ID' 
     column: 'FK_AK_TOOTH_NR_VERSION' 
     } 
    } 
} 
class ToothDisease implements Serializable{ 
    Integer idColumn 
    Integer nrVersion 
    static mapping = { 
    table name: 'AK_TOOTH_DISEASE' 
    idColumn column: 'ID', generator: 'sequence', params: [sequence: 'SEQ_AK_TOOTH_DISEASE'] 
    nrVersion column: 'NR_VERSION', 
    id composite ['idColumn','nrVersion'] 

    int hashCode() { 
    def builder = new HashCodeBuilder() 
    builder.append (idColumn) 
    builder.append (nrVersion) 
    builder.toHashCode() 
    } 

    boolean equals (other) { 
    if (other == null) { 
     return false 
    } 
    def builder = new EqualsBuilder() 
    builder.append (idColumn, other.idColumn) 
    builder.append (nrVersion, other.nrVersion) 
    builder.isEquals() 
    } 
} 

當我嘗試bootRun我的應用程序,我得到BeanCreationException:的Grails 3.1.6,休眠4.複合外鍵

Caused by: org.hibernate.MappingException: Foreign key (FK_1rnajvolkf4rkav5w1hl0l9fk:AK_TOOTH [tooth_disease_id,FK_AK_TOOTH_ID,FK_AK_TOOTH_NR_VERSION,tooth_disease_id_column,tooth_disease_nr_version])) must have same number of columns as the referenced primary key (AK_TOOTH_DISEASE [ID,NR_VERSION]). 

當我刪除了齒列映射部分,然後我得到了同樣的錯誤沒有那些列

[tooth_disease_id,tooth_disease_id_column,tooth_disease_nr_version]. 

是否有可能像hibernate中顯式定義鍵列和referencedColumnName? 爲什麼有三列?尤其是tooth_disease_id and tooth_disease_id_column columns。我試圖用「PrimaryKey的」名稱作爲牙齒和「身份證」簡單,因爲id列複合主鍵,但這樣一來,我有同樣的錯誤

Caused by: org.hibernate.MappingException: Foreign key (FK_1rnajvolkf4rkav5w1hl0l9fk:AK_TOOTH [tooth_disease_id,FK_AK_TOOTH_ID,FK_AK_TOOTH_NR_VERSION,tooth_disease_id_column,tooth_disease_nr_version])) must have same number of columns as the referenced primary key (AK_TOOTH_DISEASE [ID]).But there is only one referenced primary key. 

不幸的是,我沒有機會改變模式結構並避免組合鍵。

回答

0

我發現here從伯特·貝克威思的解決方法。 由於GrailsAnnotationConfiguration被HibernateMappingContextConfiguration.groovy代替我做了他的解決方案的一些變化:

class CompositeForeignKeyConfiguration extends HibernateMappingContextConfiguration { 
    private static final long serialVersionUID = 1 
    private static final String TOOTH_CLASS_NAME = 'com.project.TOOTH' 
    private static final String TOOTH_ID_FK = 'FK_AK_TOOTH_ID' 
    private static final String TOOTH_VERSION_FK = 'FK_AK_TOOTH_NR_VERSION' 

    private boolean _alreadyProcessed 

    @SuppressWarnings ("unchecked") 
    @Override 
    protected void secondPassCompile() throws MappingException { 

    for (PersistentEntity pc : hibernateMappingContext.getPersistentEntities()) { 
     if (pc.name == TOOTH_CLASS_NAME) { 
     pc.getAssociations().each { Association mp -> 
      if (mp.name == 'toothDisease') { 
      PropertyConfig config = (PropertyConfig) mp.getMapping().getMappedForm() 
      if (config.columns.size() == 1) { 
       config.columns.clear() 
       final ColumnConfig fk1 = new ColumnConfig (name: TOOTH_ID_FK) 
       final ColumnConfig fk2 = new ColumnConfig (name: TOOTH_VERSION_FK) 
       config.columns << fk1 
       config.columns << fk2 
      } 
      } 
     } 
     } 

    } 

    super.secondPassCompile() 
    if (_alreadyProcessed) { 
     return 
    } 
    _alreadyProcessed = true 
    } 
} 
0

我認爲這個錯誤是由你在Tooth的映射中聲明FK到ToothDisease的方式引起的。

試試這個:

class Tooth { 
    Integer id 
    ToothDisease toothDisease 

    static mapping = { 
     table name: 'AK_TOOTH' 
     id generator: 'sequence', params: [sequence: 'SEQ_AK_TOOTH'] 
     toothDisease { 
      column name: 'FK_AK_TOOTH_ID' 
      column name: 'FK_AK_TOOTH_NR_VERSION' 
     } 
    } 

} 

class ToothDisease implements Serializable{ 
    Integer idColumn 
    Integer nrVersion 

    static mapping = { 
     table name: 'AK_TOOTH_DISEASE' 
     idColumn column: 'ID', generator: 'sequence', params: [sequence: 'SEQ_AK_TOOTH_DISEASE'] 
     nrVersion column: 'NR_VERSION' 
     id composite: ['idColumn','nrVersion'] 
    } 

    int hashCode() { 
     def builder = new HashCodeBuilder() 
     builder.append (idColumn) 
     builder.append (nrVersion) 
     builder.toHashCode() 
    } 

    boolean equals (other) { 
     if (other == null) { 
      return false 
     } 
     def builder = new EqualsBuilder() 
     builder.append (idColumn, other.idColumn) 
     builder.append (nrVersion, other.nrVersion) 
     builder.isEquals() 
    } 

} 
+0

謝謝你的答案,但它didn't幫助我。與三列外鍵仍然相同的錯誤。 –