Many-to-many link tables in grails (GORM) / hibernation

I play with Grails and find the ORM stuff tedious because I don’t quite understand what I do when it comes to domain classes. I hope someone can get me back on track

Consider the following

Test task One: a lot The hardware used in the task Physical equipment: 1

... this is similar to the classic scenario Order, OrderLine, Product, considered in examples of university databases

I 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 
}

, , - , Grails , / , . , Grails hardware_on_job_job hardware_on_job_physical_hardware.

, - , . , , (HardwareOnJob), .

/ , , . Btw grails 1.2.1

+3
5

joinTable , :

, " ", " " ""

:

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']
    }

}
+4

, , . , , . , . . 5.2.1.2 5.2.1.3 this,

0

,

class Job 
{ 
  String jobName 
  String jobDescription 

  static mapping = {
     id column:"jobId"
  }

  static hasMany = [hardware:HardwareOnJob]
} 

class HardwareOnJob 
{
   String role 
   Job job
   PhysicalHardware hardware


  static mapping = {
     id column:"hardware_on_job_id"
  }

} 

class PhysicalHardware 
{ 
  String assetName 
  String model 
  String os  

  static mapping = {
     id column:"physical_hardware_id"
  }

  static hasMany = [hardwareOnjob:HardwareOnJob]
} 

? , .

, . , db , .

0

, Child , Parent

static hasMany = [children: Child]

static belongsTo = [parent: Parent]

( , , , , )

, , .

This usually creates a column in Child, holding the identifier for its parent. This may not be much for many when some kind of link table is needed (and GORM can handle it). The so-called "one-way" relationships (as you have in the first example) can also create link tables, which is slightly explained by this:

http://grails.1312388.n4.nabble.com/Many-to-many-vs-Many-to-one-td1369336.html

0
source

Source: https://habr.com/ru/post/1732220/


All Articles