【问题标题】:GORM ASSOCIATION EXPLANATIONGORM协会说明
【发布时间】:2016-07-29 07:58:32
【问题描述】:

我需要帮助创建域类之间的关联。我有三个域:用户、新闻、评论。一个用户可以在许多新闻上制作许多 cmet。一条新闻可以有来自多个用户的多个 cmets。

我在 GORM 中实现这个关联时遇到了一个大问题。

我希望用户能够将 cmets 添加到新闻项目中。评论应该有对发表评论的用户的引用。并且一个新闻项目应该有一个包含它的所有 cmets 的列表。

领域类:

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 = {
    }
}

集成测试包括:

 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
}

任何帮助将不胜感激

【问题讨论】:

  • “我的测试失败了” - 他们怎么失败了?请添加您失败的断言、堆栈跟踪、...
  • 我已经提供了测试结果。谢谢

标签: hibernate grails grails-orm


【解决方案1】:

我宁愿稍微改变你的域类:

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 = {
    }

}

在这些更改之后,您可能应该重新创建这些表(如果仍处于开发阶段,则创建-删除完整数据库)。

此外,通过在 Comment 中添加 belongsTo 语句,您应该修复您的测试(user 和 'communicate' 属性将在创建时是必需的):

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

【讨论】:

  • 你好兄弟,谢谢你的回答。我应该在我的帖子中提到这一点。新闻与用户无关。新闻本身就是一个独立的实体。它将从一个宁静的 api 中获取。然后展示给用户。然后,用户可以将他们的 cmets 添加到这些新闻项目中。请更新您的答案。谢谢
  • 此外,我将 webId 保留在 restful api 中的原因。我实现了这个以确保我的数据库中没有重复的新闻项目。 webId 是一个唯一字段。它的目的只是为了防止重复。我通常会取最新的 10 件商品。而那些重复的将引发异常,直到新的
  • 谢谢。建立联想对我来说太难了。请允许我问更多。评论属于发表评论的用户,也属于发表评论的 NewsItem。不应该belongsTo指向它们吗?
  • 事实上:是的!你可以做到这一点是二重奏。我会修正我的答案。
  • 谢谢。还要测试?。有人在我的笔记本电脑上看摔跤?。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多