How do I search for elements whose collection contains another element in Grails?

前端 未结 2 768
情深已故
情深已故 2021-02-07 21:53

Let\'s say I have a domain class called \"User\" which can follow other \"User\" objects. It does so having a field specified as:

def hasMany=[followedUsers:User         


        
相关标签:
2条回答
  • 2021-02-07 22:01

    You can use this HQL query:

    User.executeQuery(
       'select u from User u where :follower in elements(u.followedUsers)',
       [follower: userInstance])
    
    0 讨论(0)
  • 2021-02-07 22:08

    In my opinion, this syntax is much cleaner:

    User.withCriteria { followedUsers{ eq('id', userInstance.id) } }
    
    0 讨论(0)
提交回复
热议问题