代码之家  ›  专栏  ›  技术社区  ›  Blacktiger

Grails多对多关联查询

  •  2
  • Blacktiger  · 技术社区  · 16 年前

    我有很多对很多的关系。

    class Post {
        String title
        static hasMany = [tags:Tag]
    }
    
    class Tag {
        static hasMany = [posts:Post]
    }
    

    我想得到一个标签的文章列表,该标签有一些其他条件(如排序顺序、部分标题匹配等)。我做 使用Grails标准来实现这一点?或者有什么方法可以做到这一点:

    Post.findAllByTitleLikeAndTagsContains("partial title", aTag)
    
    2 回复  |  直到 14 年前
        1
  •  4
  •   Jean Barmash    16 年前

    我认为动态查找器不允许您进入一对多或多对多的关联-您必须执行一个条件或执行HQL查询路由。只能按一对一关联进行查询,不能按一对多进行查询。(见章节) 5.4.1 Dynamic Finders )

        2
  •  0
  •   Ford Guo    14 年前

    您可以使用WithCriteria,例如:

    Post.withCriteria{
        tags {
            eq 'id',aTag.id
        }
     }
    
    推荐文章