如何在grails中创建父亲孩子列表的标准

时间:2015-05-13 10:49:04

标签: grails gorm

我有父域和子域

  class Parent{
        static hasMany = [childs: Child]
  }

  class Child{

  }

在数据库中

  Parent_Id  Parent_Age  Parent_Name
      1        20          AAAA
      2        25          BBBB

   Child_id  Child_Age   Child_Name  Parent_Id(F.K.)
      1        2           00000       1
      2        6           11111       1
      3        5           22222       1 
      4        3           33333       2
      5        4           44444       2
      5        9           55555       2

现在我有了父对象。

    def parentInstance = Parent.get(1);
    def childsList     = parentInstance?.childs  ---> //from this list here  
                                                      //i want to get             
                                                     // only those childs  
                                                     //whose age is greater  
                                                     //than or equal to 3.  
                                                             Or
                                                     // greater than or 
                                                     //equal to 5.

是否可以在查询列表中执行此操作。

1 个答案:

答案 0 :(得分:0)

试试这样:

Parent parentInstance = Parent.get(1);

List<Child> childList = Child.createCriteria().list(){
     parent{
         eq("id", 1 as Long)
     }

     or{
         ge("age", 3)

         ge("age", 5)
    }

}