GORM ID generation and belongsTo association?
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', generat开发者_如何转开发or: '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 ?
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?
精彩评论