Grails Domain Class : hasOne, hasMany without belongsTo
Asked Answered
C

3

6

I am new to Grails. Can I use "hasOne" or "hasMany" without using "belongsTo" to another domain-class?

Thanks in advance.

Cruller answered 28/5, 2014 at 7:45 Comment(1)
I found this article to be perfect in explaining the different cases, and includes diagrams on what effect each has on the database: emmanuelrosa.com/articles/grails-domain-class-associationsCircumferential
O
11

Yes, you can. See examples in Grails doc: http://grails.org/doc/2.3.8/guide/GORM.html#manyToOneAndOneToOne

hasMany (without belongsTo) example from the doc:

A one-to-many relationship is when one class, example Author, has many instances of another class, example Book. With Grails you define such a relationship with the hasMany setting:

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

class Book {
    String title
}

In this case we have a unidirectional one-to-many. Grails will, by default, map this kind of relationship with a join table.

hasOne (without belongsTo) example from the doc:

Example C

class Face {
    static hasOne = [nose:Nose]
}
class Nose {
    Face face
}

Note that using this property puts the foreign key on the inverse table to the previous example, so in this case the foreign key column is stored in the nose table inside a column called face_id. Also, hasOne only works with bidirectional relationships.

Finally, it's a good idea to add a unique constraint on one side of the one-to-one relationship:

class Face {
    static hasOne = [nose:Nose]
    static constraints = {
        nose unique: true
    }
}

class Nose {
    Face face
}
Organelle answered 28/5, 2014 at 7:53 Comment(2)
Thank you. heikkim. It helps a lot to clear the concept! This is feasible and link is authentic!Cruller
The documentation link is broken. => grails.github.io/grails2-doc/2.3.8/guide/… or for the latest doc: gorm.grails.org/latest/hibernate/manual/#gormAssociationDickenson
C
7

Yes you can, but it behave differently

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

    class Book {
        String title

    }

In this case if you delete Author the books still existing and are independent.

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

 class Book {
        String title
      static belongsTo = [author: Author]
    }

In this other case if you delete the Author it will delete all the books pf that author in cascade.

Many-to-one/one-to-one: saves and deletes cascade from the owner to the dependant (the class with the belongsTo).

One-to-many: saves always cascade from the one side to the many side, but if the many side has belongsTo, then deletes also cascade in that direction.

Many-to-many: only saves cascade from the "owner" to the "dependant", not deletes.

http://grails.org/doc/2.3.x/ref/Domain%20Classes/belongsTo.html

Canary answered 28/5, 2014 at 7:58 Comment(0)
S
2

yes very easy like a class defintion but only specify hasMany but no need for hasOne

class Student {

String name

User userProfile

static hasMany =[files:File]
}


class User {

String uname

Student student

} 


class File {

String path 

Student student  // specify the belongs to like this no belong to 


}

Done!!

Scoundrelly answered 28/5, 2014 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.