Why does GORM use a one-to-many join table?

From the Grails documentation , by default, one-to-many relationships are represented using a join table.

I do not understand why this is desirable. I had little SQL experience before starting to use Hibernate and / or GORM Grails. It seems that using a foreign key in a many table pointing to a row in a one table is a way to implement a one-to-many relationship ...

Can someone explain such a solution?

+4
source share
1 answer

The reason for using a join table for a one-to-many unidirectional relationship is that many aspects of a relationship can have many relationships and are not aware of these relationships. Perhaps the best example is here:

class Book {
  String title
}

class BookStore {
  String name
  static hasMany = [books: Book]
}

class Library {
  String name
  static hasMany = [books: Book]
}

In the above a domain Bookneed not have IDs BookStore, and Libraryon the table. A Bookworks great without them. Using join tables, this prevents foreign books from polluting the book table.

Keep in mind that this is a modeling of unidirectional rather than bidirectional relationships.

+2
source

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


All Articles