grails(GORM)/ hibernate中的多对多链接表 [英] Many-to-Many link tables in grails (GORM) / hibernate

查看:110
本文介绍了grails(GORM)/ hibernate中的多对多链接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和Grails玩,我发现ORM的东西很乏味,因为我没有完全理解我在做域类时做了什么。我希望有人能让我回到正轨上



考虑以下内容:

测试工作一:许多硬件用于作业许多:一个物理硬件



...类似于传统Order,OrderLine,在大学数据库示例中看到的产品方案



我创建了以下域类

  class Job 
{
String jobName
String jobDescription
}
$ b $ class HardwareOnJob
{
static hasMany = [作业:作业,physicalHardware:PhysicalHardware]
static belongsTo =作业

字符串角色
}

类PhysicalHardware
{
字符串assetName
字符串模型
字符串os
}

我需要问的是,为什么Grails会在我的数据库中为我创建两个额外的表格,而不是使用我定义的链接实体/域类。例如,Grails会在数据库中创建hardware_on_job_job和hardware_on_job_physical_hardware。

使用脚手架控制器,我可以输入一些硬件,输入工作,然后将两者连接在一起。我的问题是为什么它会创建这两个额外的表,而不是使用我指定的域对象(HardwareOnJob)。

任何帮助/指导都会非常感激,因为会坚持看着这个并尝试新事物。顺便说一下,我在grails版本1.2.1上

解决方案

查看 joinTable 关键字其中:


自定义用于单向一对多,多对多和原始集合类型的连接表



以下是用户指南中的示例:

  class Book {
字符串标题
静态belongsTo =作者
静态hasMany = [作者:作者]

静态映射= {
作者joinTable:[name:mm_author_books, key:'mm_book_id']
}
}
class作者{
字符串名称
static hasMany = [books:Book]

static如果您的图书馆的图书馆名称为:[
books joinTable:[name:mm_author_books,key:'mm_author_id']
}

}
pre>

I'm playing aroud with Grails and am finding the ORM stuff tedious because I don't fully understand what I'm doing when it comes to domain classes. I'm hoping someone can put me back on track

Consider the following

Test Job One:Many Hardware Used on Job Many:One Physical Hardware

...this is analogous to the classic Order, OrderLine, Product scenario seen in university DB examples

I've created the following domain classes

class Job
{
  String jobName
  String jobDescription
}

class HardwareOnJob
{
   static hasMany = [  jobs:Job, physicalHardware:PhysicalHardware ]
   static belongsTo = Job

   String role
}

class PhysicalHardware
{
  String assetName
  String model
  String os 
}

The question I need to ask is why does Grails create me two extra tables in my database rather than using the link entity/domain class I've defined. For instance Grails creates hardware_on_job_job and hardware_on_job_physical_hardware in the database.

Using the scaffolded controllers I can enter some hardware, enter a job and then enter link the two together. The question I have is why does it create these two extra tables rather than use the domain object (HardwareOnJob) I've specified.

Any help/guidance would be very much appreciated as going nuts looking at this and trying new things. Btw I'm on grails version 1.2.1

解决方案

Have a look at the joinTable keyword which:

Customizes the join table used for undirectional one-to-many, many-to-many and primitive collection types

Here is the example from the user guide:

class Book {
    String title
    static belongsTo = Author
    static hasMany = [authors:Author]

    static mapping = {
        authors joinTable:[name:"mm_author_books", key:'mm_book_id' ]
    }
}
class Author {
    String name
    static hasMany = [books:Book]

    static mapping = {
        books joinTable:[name:"mm_author_books", key:'mm_author_id']
    }

}

这篇关于grails(GORM)/ hibernate中的多对多链接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆