【发布时间】: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