简体   繁体   English

Grails-在CreateCriteria中使用逻辑或

[英]Grails - Use logical OR in CreateCriteria

Starting with the following Domains: 从以下域开始:

class Person {
    String login
    String name 
}

class MyDomain {
    Person creator
    static hasMany =  [userlist:Person]
}

I wrote a critera to retreive all MyDomainIntances where I'm as creator OR where I'm in the userlist: 我编写了一个critera来检索我作为创建者或用户列表中所有MyDomainIntances的位置:

def login = "myself"

def result =  MyDomain.createCriteria().list () {
    projections { distinct ( "id" )
        property("name")
        property("id")
    }
    or {             
       userlist{eq("login", login)}
       creator {eq("login",login)}
    }

    order("name","desc")
}

The problem is with this code I get only instances where I'm in the userlist. 问题在于此代码仅获得我在用户列表中的实例。

Though creator {eq("login",login)} works well: if I use it itself only, I get a list where I'm as creator 尽管creator {eq("login",login)}运作良好:如果仅使用它本身,我会得到一个列表,其中我是创建者


generated query: 生成的查询:

Hibernate:
select distinct this_.id as y0_,this_.name as y1_,this_.id as y2_
from mydomain this_ inner join person creator_al2_ on this_.creator_id=creator_al2_.id
                    inner join mydomain_person userlist5_ on this_.id=userlist5_.mydomain_userlist_id
                    inner join person userlist1_ on userlist5_.person_id=userlist1_.id
where ((userlist1_.login=?) or (creator_al2_.login=?))
order by this_.name desc 

I think the issue is in the OR, as you state that you only get results where 'myself' is in the user list. 我认为问题出在OR中,因为您声明只有在用户列表中有“我自己”的地方才能得到结果。 This blog may help, which also cites this helpful article on createAlias. 该博客可能会有所帮助,这也会引用有关createAlias的有用文章

def result =  MyDomain.createCriteria().list () {
    createAlias('creator', 'c')

    projections { distinct ( "id" )
        property("name")
        property("id")
    }

    or {             
       userlist{
           eq("login", login)
       }
       eq("c.login", login)
    }

    order("name","desc")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM