pri_dev
pri_dev

Reputation: 11645

one to many mapping using gorm

I am having a domain class arHistory as follows:

package ars
import ars.AccessRequest
import gra.Users
class ArHistory {
    Long id =2340
    Users updatedby
    Date updatedon
    String requeststatus  
    static hasMany=[accessrequests:AccessRequest]

    static constraints = {
         requeststatus(blank:false, nullable:false)
    }

Now after I run the application the GORM create tables ar_history and ar_history_access_request (the join table for one to many relationship)

The join table above has only 2 foreign keys, the table itself has no primary key id I wanted to know 3 things, 1) do I need to have a primary key id for the join table 2) if yes how do I create the id (do I create it manually through mysql) 3) whats the advantage of having hasMany() instead of having a class variable AccessRequest defined in ArHistory, is it just normalised data?

Regards Priyank

Upvotes: 0

Views: 376

Answers (1)

Tomasz Kalkosiński
Tomasz Kalkosiński

Reputation: 3723

You don't need primary key in join table, since you don't allow duplicates with hasMany. Read the documentation of hasMany: "Grails will automatically inject a property of type java.util.Set into the domain class based on the hasMany setting". Thus, primary key is not needed.

What do you mean by third question? hasMany allows you to add many AccessRequest objects to a collection.

Upvotes: 1

Related Questions