Grails/GORM:没有方法签名适用于参数类型

Dim*_*vos 0 grails grails-orm

我正在尝试使用 Grails 2.4.4 组合一个基本的博客应用程序。我的域模型如下:

class Commentable {
  String title
  static hasMany = [comments:Comment]   
}

class Comment extends Commentable {
  static belongsTo = [target:Commentable]
}

class Post extends Commentable {
  static hasMany = [tags:Tag]
}

class Tag {
  String label
  static hasMany = [posts:Post]
  static belongsTo = Post
}
Run Code Online (Sandbox Code Playgroud)

在 BootStrap.groovy 的 init 方法中,我试图创建一个 Post 和一个 Tag 如下

def post = new Post();
post.setTitle("Post1");
post.save();

def tag = new Tag();
tag.setLabel("Tag1");
tag.save();

tag.addToPost(post);
tag.save();
Run Code Online (Sandbox Code Playgroud)

这会产生以下错误消息:

Message: No signature of method: io.dimitris.blog.Tag.addToPost() is 
applicable for argument types: (io.dimitris.blog.Post) values: 
[io.dimitris.blog.Commentable : 1]
Possible solutions: addToPosts(java.lang.Object)
Run Code Online (Sandbox Code Playgroud)

任何关于我做错了什么的提示都将不胜感激。

Jef*_*own 5

您正在打电话,tag.addToPost(post)但您需要tag.addToPosts(post). 该hasMany物业是static hasMany = [posts:Post]。该映射中的键指示方法名称。如果您将其更改为,static hasMany = [post:Post]则方法将为addToPost(post),但名称将没有意义。