如何强制 grails GORM 尊重 DB 方案?

发布于 2024-09-01 06:29:39 字数 1273 浏览 10 评论 0原文

我有两个域:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-09-08 06:29:39

如果您更改belongsTo 声明,它就会起作用。您可以在belongsTo 中命名该实例,而不是仅仅拥有对CodeSet id 的引用,然后您将获得对该实例的引用并避免连接表。我还删除了多余的映射:

class Cartridge {

   String id
   Date runDate

   static belongsTo = [codeSet: CodeSet]

   static mapping = {
      version false
      id generator: 'assigned'
      codeSet column:'code_set_id'
   }
}

class CodeSet { 

   String id
   String owner
   String comments
   String geneRLF
   String systemAPF

   static hasMany = [cartridges:Cartridge]

   static mapping = {
      version false
      id generator: 'assigned'
      geneRLF column:'gene_rlf'
      systemAPF column:'system_apf'
   }
}

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:

class Cartridge {

   String id
   Date runDate

   static belongsTo = [codeSet: CodeSet]

   static mapping = {
      version false
      id generator: 'assigned'
      codeSet column:'code_set_id'
   }
}

class CodeSet { 

   String id
   String owner
   String comments
   String geneRLF
   String systemAPF

   static hasMany = [cartridges:Cartridge]

   static mapping = {
      version false
      id generator: 'assigned'
      geneRLF column:'gene_rlf'
      systemAPF column:'system_apf'
   }
}
番薯 2024-09-08 06:29:39

我使用的是双向一对多模式,但我也可以使用单向模式。

因此,对于域 CodeSet,修复方法是:

class CodeSet { 

  String id
  String owner
  String comments
  String geneRLF
  String systemAPF

  Cartridge cartridge

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

    static mapping = {
      table 'code_set'
      version false
      id column:'code_set_id', generator: 'assigned'
      columns {
         owner:'owner'
         comments:'comments'
         geneRLF:'gene_rlf'
         systemAPF:'system_apf'
      }
  }

但是,我仍然对双向和单向模式感到困惑?
有人可以给我举一个很好的例子(帮助我理解)吗?
谢谢

I was using a bidirectional one-to-many mode, but I can also use an unidirectional mode.

So for domain CodeSet, the fix is :

class CodeSet { 

  String id
  String owner
  String comments
  String geneRLF
  String systemAPF

  Cartridge cartridge

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

    static mapping = {
      table 'code_set'
      version false
      id column:'code_set_id', generator: 'assigned'
      columns {
         owner:'owner'
         comments:'comments'
         geneRLF:'gene_rlf'
         systemAPF:'system_apf'
      }
  }

But, I still confuse with bidirectional and unidirectional mode ?
Somebody, can show me a good example (to help me to understand) ?
Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文