如何强制 grails GORM 尊重 DB 方案?
我有两个域:
class CodeSet {
String id
String owner
String comments
String geneRLF
String systemAPF
static hasMany = [cartridges:Cartridge]
static constraints = {
id(unique:true,blank:false)
}
static mapping = {
table 'code_set'
version false
columns {
id column:'code_set_id', generator: 'assigned'
owner column:'owner'
comments column:'comments'
geneRLF column:'gene_rlf'
systemAPF column:'system_apf'
}
}
and :
class Cartridge {
String id
String code_set_id
Date runDate
static belongsTo = CodeSet
static constraints = {
id(unique:true,blank:false)
}
static mapping = {
table 'cartridge'
version false
columns {
id column:'cartridge_id', generator: 'assigned'
code_set_id column:'code_set_id'
runDate column:'run_date'
}
}
实际上,通过这些模型,我得到了表格:
- 代码集,
- 墨盒,
- 和表:code_set_cartridge(两个字段:code_set_cartridges_id、cartridge_id)
我不想有 code_set_cartridge 表,但保持关系:
代码集 --> 1:n-->换句话说
,如何在没有中间表的情况下保持 code_set 和 Cartridge 之间的关联? (使用 code_set_id 作为 code_set 中的主键,使用 code_set_id 作为卡带中的外键)。
用GORM映射不需要中间表就可以完成吗?
I have two domains :
class CodeSet {
String id
String owner
String comments
String geneRLF
String systemAPF
static hasMany = [cartridges:Cartridge]
static constraints = {
id(unique:true,blank:false)
}
static mapping = {
table 'code_set'
version false
columns {
id column:'code_set_id', generator: 'assigned'
owner column:'owner'
comments column:'comments'
geneRLF column:'gene_rlf'
systemAPF column:'system_apf'
}
}
and :
class Cartridge {
String id
String code_set_id
Date runDate
static belongsTo = CodeSet
static constraints = {
id(unique:true,blank:false)
}
static mapping = {
table 'cartridge'
version false
columns {
id column:'cartridge_id', generator: 'assigned'
code_set_id column:'code_set_id'
runDate column:'run_date'
}
}
Actually, with those models, I get tables :
- code_set,
- cartridge,
- and table : code_set_cartridge (two fields : code_set_cartridges_id, cartridge_id)
I would like to not have code_set_cartridge table, but keep relationship :
code_set --> 1:n --> cartridge
In other words, how can I keep association between code_set and cartridge without intermediate table ? (using code_set_id as primary key in code_set and code_set_id as foreign key in cartridge).
Mapping with GORM can be done without intermediate table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您更改belongsTo 声明,它就会起作用。您可以在belongsTo 中命名该实例,而不是仅仅拥有对CodeSet id 的引用,然后您将获得对该实例的引用并避免连接表。我还删除了多余的映射:
It works if you change the belongsTo declaration. Rather than having just a reference to the CodeSet's id, you can name the instance in the belongsTo and you'll get the reference to the instance and avoid the join table. I also removed redundant mappings:
我使用的是双向一对多模式,但我也可以使用单向模式。
因此,对于域 CodeSet,修复方法是:
但是,我仍然对双向和单向模式感到困惑?
有人可以给我举一个很好的例子(帮助我理解)吗?
谢谢
I was using a bidirectional one-to-many mode, but I can also use an unidirectional mode.
So for domain CodeSet, the fix is :
But, I still confuse with bidirectional and unidirectional mode ?
Somebody, can show me a good example (to help me to understand) ?
Thanks