简体   繁体   中英

Grails - criteria query for assoociation

I have domain model like

class Event{

 ...

static belongsTo = [user:User]

} 

and

class User{
  ...
  static hasMany = [events :Event];

}

Now I am simply want to search all events by specified user id. How can I do this? I'm not able to find any example.

This can be done by using the back-reference which is created by "belongsTo".

Simply create a criteria as normal, and use the User Domain object in the criteria to search for the ID or other properties:

Event.createCriteria().list {
     User {
          eqId(1 as Long) //The id of the user you're searching for.
     }
}

Hope that helps!

这是一个简单的用例,您可以通过以下方式找到特定用户的所有事件:获取当前用户,然后

def eventList = user?.events

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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