Add dateCreated field to join table in Grails

I have an M: M relationship between a user and an icon that creates a connection table called user_badges. There are fields in this table: user_id and badge_id. Is there a way to get the standard date_created fields in this table?

class Badge { static belongsTo = User static hasMany = [users: User] } class User { static hasMany = [badges: Badge] } 
+4
source share
3 answers

Basically, you need to change the mapping so that the M: M relation is expressed as two 1: M. relations. Here is an example where the join class is BadgeOwner (therefore, by default the generated join table will be called badge_owner )

 class Badge { static hasMany = [owners: BadgeOwner] } class User { static hasMany = [owners: BadgeOwner] } class BadgeOwner { static belongsTo = [user: User, badge: Badge] Date dateCreated Date lastUpdated } 
+6
source

If it has additional properties, this is not a join table. This is a separate facility. So, match it accordingly :-)

+4
source

I am doing the same research. Maybe this is the best way. Please comment below if I am wrong.

 class Badge { /* Declare Variables */ static belongsTo = [BadgeOwner] } class User { /* Declare Variables */ static belongsTo = [BadgeOwner] } class BadgeOwner { User user Badge badge Date dateCreated Date lastUpdated } 
0
source

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


All Articles