Several rake associations

I have a grails app with a domain restaurant and domain staff.

class Restaurant {
  String name

  static belongsTo = [ owner: Person ]
}

class Person {
  String name

  static hasMany = [ favoriteRestaurants : Restaurant ]
}

My problem is that GORM only creates two tables: Restaurant and Person, where the restaurant has owner_id. However, what I miss is a connection table that links my favorite restaurants to it.

I can understand why GORM does it this way (bidirectional one-to-many), however I cannot figure out how to do it the way I want (1x unidirectional unidirectional, 1x unidirectional ellipses - one). I suppose I should use mappedBy , but I don't know how to match it, since there is nothing binding it: - (

In addition, I initially considered the following areas:

class Restaurant {
  String name

  static belongsTo = [ owner: Person ]
  static hasMany = [ outstandingCouponOwners : Person ]
}

class Person {
  String name

  static hasMany = [ favoriteRestaurants : Restaurant ]
}

" " ( , )

+3
1

, mappedBy . . 5.2.1.2 Grails. , Person hasMany: , . ( ) :

class Restaurant {
  String name

  static belongsTo = [ owner: Person ]
  static hasMany = [ outstandingCouponOwners : Person ]
}

class Person {
  String name

  static hasMany = [ favoriteRestaurants : Restaurant, owns: Restaurant, coupons: Restaurant ]
  static mappedby = [ owns: 'owner', coupons: 'outstandingCouponOwners' ]
}
+2

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


All Articles