views:

48

answers:

1

I have two domains :

class CodeSetDetail {

  String id
  String codeSummaryId

  static hasMany = [codes:CodeSummary]

    static constraints = {
      id(unique:true,blank:false)
    }

    static mapping = {
      version false
      id column:'code_set_detail_id', generator: 'assigned'
    }
}

and :

class CodeSummary {

  String id
  String codeClass
  String name
  String accession

  static belongsTo = [codeSetDetail:CodeSetDetail]

    static constraints = {
      id(unique:true,blank:false)
    }

    static mapping = {
      version false
      id column:'code_summary_id', generator: 'assigned'
    }
}

I get two tables with columns:

code_set_detail:

code_set_detail_id     
code_summary_id 

and

code_summary:

code_summary_id 
code_set_detail_id (should not exist)    
code_class    
name   
accession 

I would like to link code_set_detail table and code_summary table by 'code_summary_id' (and not by 'code_set_detail_id').
Note : 'code_summary_id' is define as column in code_set_detail table, and define as primary key in code_summary table.

To sum-up, I would like define 'code_summary_id' as primary key in code_summary table, and map 'code_summary_id' in code_set_detail table.

How to define a primary key in a table, and also map this key to another table ?

+1  A: 

From your Groovy code, each CodeSetDetail has many CodeSummary objects associated with it. The way to do this in the DB is to have each row of code_summary table identify a row it is associated with from code_set_detail table. You said:

To sum-up, I would like define 'code_summary_id' as primary key in code_summary table, and map 'code_summary_id' in code_set_detail table.

If you have a code_summary_id in code_set_detail table, each row in code_set_detail can be associated to at-most one row in code_summary table, which means that in Groovy each CodeSetDetail object can point to at most one CodeSummary object. Is that what you want?

binil
I would like to link : CodeSetDetail(codeSummaryId=TEM123) with CodeSummary(codeSummaryId=TEM123).
Fabien Barbier
In that case a `CodeSetDetail` can refer to only one `CodeSummary`. If that is what you want remove the lines `String codeSummaryId` and `static hasMany = [codes:CodeSummary]` from `CodeSetDetail` and just have a `CodeSummary codeSummary` field instead. Also refer to the One-to-One section at http://www.grails.org/GORM+-+Defining+relationships
binil
I can add use static belongsTo = [codeSetDetail:CodeSetDetail], if I want to cascaded deletion. Thanks.
Fabien Barbier