多个与Grails中的相同域类有许多关系 [英] Multiple hasMany relationships to same domain class in Grails

查看:119
本文介绍了多个与Grails中的相同域类有许多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <$ 

使用Grails,我有一个具有多个hasMany属性的域模型到同一个域类。 c $ c> static hasMany = [posts:Post,likes:Post,dislikes:Post]

我遇到的问题是,当我在帖子列表中添加一些内容时,它也会以某种方式将它列入喜欢和不喜欢的列表中。至少,当我遍历每个列表时,这就是它的样子。



我认为问题在于我的Post域中还存在以下关系:

  static belongsTo = [contributer:Contributer] 

配置这些关系以使我的模型起作用的最佳方法是什么?任何建议?






@Wayne,



你的测试也是成功的。所以,我能想到的唯一情况是我的PostController中的保存方法有问题。我粘贴了下面的relavent代码(我使用的是Spring Security Core插件,而我的Contributer类扩展了使用该插件创建的User类):

  @Secured(['IS_AUTHENTICATED_FULLY'])
def save = {
def props = [title:params.title,post:params.post,category:Category.get params.category.id)]

def user = Contributer.get(springSecurityService.principal.id)
def postInstance = new Post(道具)

postInstance。 contributer = user
if(postInstance.save(flush:true)){
flash.message =$ {message(code:'default.created.message',args:[message(code:' post.label',default:'Post'),postInstance.id])}
redirect(action:show,id:postInstance.id)
}
else {
render(view:create,model:[postInstance:postInstance])
}
}

这里有什么突出的东西吗?

解决方案

问题是t你在Post和Contributor之间有一对多的关系(帖子有一个作者,作者有很多帖子),以及Post和Contributor之间的多对多关系(帖子中有许多相似者,像许多帖子一样)(帖子中有很多不喜欢的人,不喜欢很多职位)。 Post中的 belongsTo 确实解释了行为,但移除它并不能解决问题,只需创建不同的问题。最终的结果是,GORM惯例将不足,所以你必须告诉GORM如何表现或模拟事物。



有几种选择,但是跳转到脑海中就是将投票与Post单独建模,并使之成为贡献者 hasMany likeVotes和 hasMany dislikeVotes p>

  class Vote {

//在这里举例说明,您需要考虑
/ /级联行为,如果您决定
// //走这条路线,那么这种行为是有道理的,并且是模型化的。
belongsTo = [投稿,贡献者]

}

类LikeVote extends投票{
}

class DislikeVote extends Vote {
}

GORM会将此模型设置为带有鉴别器列的一个投票表,并且不喜欢;这可以让你消除喜欢,不喜欢和创作的帖子之间的冲突。



然后在贡献者

  hasMany = [likes:LikeVote,dislikes :DislikeVote,posts:Post] 

现在已经清除关系:


  1. 发布有许多likeVotes

  2. 发表了许多不喜欢的评论

  3. / li>
  4. 贡献者有很多不喜欢的评论

  5. 有一个贡献者

  6. $ b

GORM可以理解这些关系,并且行为适当。

如果您不喜欢这个选项,下一步就是为您的数据库结构指定自定义映射,然后使用 mappedBy 来区分各种关系。如果您绝对想要以三种不同方式直接将发布者与帖子关联起来,则需要采取这种方法。

I'm using Grails, and I have a domain model with multiple hasMany attributes to the same domain class, which looks like this:

static hasMany = [ posts : Post, likes : Post, dislikes : Post ]

The problem that I'm running into is that when I add something to the posts list, it also somehow makes it into the likes and dislikes lists. At least, that's how it looks when I iterate through each of those lists.

I think that the issue is that I also have the following relationship in my Post domain:

static belongsTo = [ contributer : Contributer ]

What is the best way of going about configuring these relationships to make my model work? Any suggestions?


@Wayne,

I tried using your test as well, and it passed successfully. So, the only thing that I can think of is that there is something wrong with my save method in my PostController. I have pasted the relavent code below (I am using the Spring Security Core plugin, and my Contributer class extends the User class that is created with that plugin):

@Secured(['IS_AUTHENTICATED_FULLY'])
def save = {
def props = [title:params.title, post:params.post,   category:Category.get(params.category.id)]

def user = Contributer.get(springSecurityService.principal.id)
def postInstance = new Post(props)

postInstance.contributer = user
if (postInstance.save(flush: true)) {
  flash.message = "${message(code: 'default.created.message', args: [message(code: 'post.label', default: 'Post'), postInstance.id])}"
  redirect(action: "show", id: postInstance.id)
}
else {
  render(view: "create", model: [postInstance: postInstance])
}
}

Is there anything that stands out here?

解决方案

The problem is that you have a one to many between Post and Contributor (post has an author, author has many posts) as well as two many to many relationships between Post and Contributor (post has many likers, likers like many posts) (post has many dislikers, dislikers dislike many posts). The belongsTo in Post does explain the behavior, but removing it will not fix the problem, just create different ones. The end result is that GORM conventions are going to fall short so you have to tell GORM how to behave or model things differntly.

There are several options, but the one that jumps to mind is to model Vote separately from Post and make it so that a Contributor hasMany likeVotes and hasMany dislikeVotes

class Vote {

   // for illustration here, you need to think about the 
   // cascading behavior that makes sense and model it if you decide 
   // to go this route. 
  belongsTo = [post, contributor] 

}

class LikeVote extends Vote {
}

class DislikeVote extends Vote {
}

GORM will model this as one vote table with a discriminator column to separate likes and dislikes; this will let you eliminate the conflicts between likes, dislikes, and authored posts.

Then in Contributor

 hasMany = [likes:LikeVote, dislikes:DislikeVote, posts:Post]

The relationships are cleared up now:

  1. Post has many likeVotes
  2. Post has many dislikeVotes
  3. Contributor has many likeVotes
  4. Contributor has many dislikeVotes
  5. Post has one contributor
  6. Contributor has many posts

GORM can understand these relationships and will behave appropriately.

If you don't like this option, the next step would be to specify custom mappings for your database structure and then use mappedBy to differentiate the various relationships. This is the approach to take if you absolutely want to have a Contributor relate directly to Post in three different ways.

这篇关于多个与Grails中的相同域类有许多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆