0

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:

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


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 
Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
Jils
  • 783
  • 5
  • 12
  • 32

1 Answers1

1

I think the issue is in the OR, as you state that you only get results where 'myself' is in the user list. This blog may help, which also cites this helpful article on 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")
}
John
  • 10,837
  • 17
  • 78
  • 141