Wednesday, July 2, 2014

Relationships Between Domain Classes Grails

Relationships Between Domain Classes     Grails main page
  • There are two static variables that define relationships between domain classes: 
    • belongsTo  
    • hasMany
  • For example, if there is a class Company, and there are many Employees in a Company, you say the following:
  • Put this in the Company class
    • static hasMany = [employees: Employee]
  • Put this in the Employee class:
    • static belongsTo = Company
    • Company company
  • As mentioned before, constraints tell the order that fields get displayed.  So in the in the Company class you would say:
    static constraints = {
                    ....
                    employees()
                    ....
    }
  • The belongsTo also tells Grails that the Employee instance is owned by the Company instance, so that when a Company is deleted, all Employees are automatically deleted also.
  • Using these two static declarations, you can describe the following relationships.
    one-to-many
    many-to-one
    one-to-one

0 comments:

Post a Comment