createcriteriagrails

createcriteriagrails,grails,Grails,我试图在grails中使用createcriteria方法,但是我得到了一个空列表,我不知道为什么。 我的代码如下 def results = PostOrder.createCriteria().list() { posts{ author{ eq('username', lookupPerson().username) } } picture{

我试图在grails中使用createcriteria方法,但是我得到了一个空列表,我不知道为什么。 我的代码如下

    def results = PostOrder.createCriteria().list() {
        posts{
            author{
                eq('username', lookupPerson().username)
            }
        }
        picture{
            user{
                eq('username', lookupPerson().username)
            }
        }
    }
PostOrder域如下所示:

class PostOrder {

    String pOrder
    Date dateCreated
    Picture picture
    Post posts
    Video video
    Boolean favorite = false

    static hasMany = [children : Child]

    static constraints = {
        picture nullable: true
        posts nullable: true
        video nullable:  true
    }
}
 class Post {

    String message
    User author
    Date dateCreated
    Child child
    boolean postedToAll
    String tag

static hasMany = [tags:Tag]

    static constraints = {
        child nullable: true
        tags nullable: true
        tag nullable: true
     }
}
 class Picture {

    String orgName
    String urlOrg
    String urlWeb
    String urlThumb
    Date   dateCreated
    String caption
    Child child
    User user
    Album contained
    String tag
    boolean postedToAll

    static hasMany = [tags:Tag]

    static constraints = {
        orgName blank: false
        caption maxSize: 500
        tags nullable:  true
        caption nullable: true
        tag nullable: true
        child nullable:  true
    }
}
职位如下:

class PostOrder {

    String pOrder
    Date dateCreated
    Picture picture
    Post posts
    Video video
    Boolean favorite = false

    static hasMany = [children : Child]

    static constraints = {
        picture nullable: true
        posts nullable: true
        video nullable:  true
    }
}
 class Post {

    String message
    User author
    Date dateCreated
    Child child
    boolean postedToAll
    String tag

static hasMany = [tags:Tag]

    static constraints = {
        child nullable: true
        tags nullable: true
        tag nullable: true
     }
}
 class Picture {

    String orgName
    String urlOrg
    String urlWeb
    String urlThumb
    Date   dateCreated
    String caption
    Child child
    User user
    Album contained
    String tag
    boolean postedToAll

    static hasMany = [tags:Tag]

    static constraints = {
        orgName blank: false
        caption maxSize: 500
        tags nullable:  true
        caption nullable: true
        tag nullable: true
        child nullable:  true
    }
}
最后图片如下:

class PostOrder {

    String pOrder
    Date dateCreated
    Picture picture
    Post posts
    Video video
    Boolean favorite = false

    static hasMany = [children : Child]

    static constraints = {
        picture nullable: true
        posts nullable: true
        video nullable:  true
    }
}
 class Post {

    String message
    User author
    Date dateCreated
    Child child
    boolean postedToAll
    String tag

static hasMany = [tags:Tag]

    static constraints = {
        child nullable: true
        tags nullable: true
        tag nullable: true
     }
}
 class Picture {

    String orgName
    String urlOrg
    String urlWeb
    String urlThumb
    Date   dateCreated
    String caption
    Child child
    User user
    Album contained
    String tag
    boolean postedToAll

    static hasMany = [tags:Tag]

    static constraints = {
        orgName blank: false
        caption maxSize: 500
        tags nullable:  true
        caption nullable: true
        tag nullable: true
        child nullable:  true
    }
}

对我来说,这很好用,有人能理解为什么is没有吗?

它在图片和帖子中的用户名都相同吗???
如果没有,则必须用and或{}包围它们,因为默认情况下使用and逻辑

可能您应该添加如下逻辑块(and/or):

def results = PostOrder.createCriteria().list() {
    or {
        posts{
            author{
                eq('username', lookupPerson().username)
            }
        }
        picture{
            user{
                eq('username', lookupPerson().username)
            }
        }
    }
}

你想找到什么?尝试删除帖子或图片并查看结果。我希望获得所有PostOrder实例,其中帖子中的作者等于当前用户,图片中的用户等于当前用户。请确保您有一篇包含图片的帖子。启用HibernateSQL输出也很好,然后您可以看到GORM正在运行的查询。这可以在
DataSource.groovy
中完成,方法是在
DataSource{}
块中添加
logSql=true
。您得到的是一个空列表-您确定答案是错误的吗?i、 e.您是在单元/集成测试中运行此查询,还是在实际数据库中运行此查询?您是否有与正在运行的查询实际匹配的数据?