Clinton Yeboah
Clinton Yeboah

Reputation: 126

GORM ASSOCIATION EXPLANATION

I need help creating association among Domain Classes. I have three domains: User, News, Comments. A User can make many comments on many news. A news can have many comments from many users.

I am having a big problem implementing this association in GORM.

Gorm Assocaition

I want to be able for a user to add comments to a news item. The comment should have a reference to the user who made it. And a news item should have a list of all its comments.

Domain Classes:

class User {
    String nickName
    String password
    Date dateCreated
    static hasMany = [users : User, comments : Comment]
    static hasOne = [profile : Profile]
    //static belongsTo = [comments : Comment]

    static constraints = {
        profile nullable: true
    }
}

class News {

    String webTitle
    String webPublicationDate;
    String trailText;
    String webUrl;
    String thumbnail;
    String webId;
    static hasMany = [tags : Tag, comments  : Comment]

    static constraints = {
    }
}

class Comment {
    //static belongsTo = [news : News, user : User]
    String content
    int likes
    int disLikes
    Date lastUpdated
    static hasOne = [user: User, news:  News]

    static constraints = {
    }
}

IntegrationTests include:

 void "A user can add to a post's comments"(){
    given: "An existing user"
    def newUser = new User(nickName: 'nickName', password: 'password').save(failOnError: true, flush : true)

    and: "A comment and a news"
    def news = new NewsItem(webTitle: 'I Love Hillary', webPublicationDate: new Date(),
            trailText: 'Hillary can change America', webUrl: 'http:google.com', webId: '4', thumbnail: 'image')

    when: 'A user adds comment to post'
    def comm = new Comment(content: "Hillary cant be president", user: newUser, newsItem: news)
    newUser.addToComments(comm)
    news.addToComments(comm)

    then: "NewsItem must have some comments"
    news.comments.size() == 1
    news.comments[0].user == newUser
    newUser.comments.size() == 1
    comm.user == newUser
    news.comments != null
}

Any help will be appreciated

Upvotes: 1

Views: 162

Answers (1)

Michal_Szulc
Michal_Szulc

Reputation: 4177

I'd rather slightly change your domain classes:

class User {
    String nickName
    String password
    Date dateCreated
    static hasMany = [users : User, comments : Comment]
    static hasOne = [profile : Profile]

    static constraints = {
        profile nullable: true
    }
}

class Communicate { //because News could possibly create troubles during generating controller or test classes (it's the same as singular and plural)
    String webId; 
    String webTitle
    String webPublicationDate;
    String trailText;
    String webUrl;
    String thumbnail;
    static hasMany = [tags : Tag, comments  : Comment]

    static constraints = {
    }

}

class Comment {
    static belongsTo = [user: User, communicate: Communicate] //read more about it: http://docs.grails.org/latest/ref/Domain%20Classes/belongsTo.html
    String content
    int likes
    int disLikes
    Date lastUpdated

    static constraints = {
    }

}

After these changes you should probably recreate these tables (or create-drop full db if it's still in dev stage).

Moreover by adding belongsTo statements to Comment you should fix your tests (user and 'communicate' properties will be neccessary on create-time):

def comm = new Comment(content: "Hillary cant be president", user:user, communicate:news)

Upvotes: 1

Related Questions