如何在grails中创建父级子列表的条件

时间:2021-03-16 23:36:28

I have Parent Domain and a Child Domain

我有父域和子域

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

  class Child{

  }

In database

  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

Now I have parent object.

现在我有了父对象。

    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.

Is it possible to do that on queried list .

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

1 个解决方案

#1


Try it like this:

试试这样:

Parent parentInstance = Parent.get(1);

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

     or{
         ge("age", 3)

         ge("age", 5)
    }

}

#1


Try it like this:

试试这样:

Parent parentInstance = Parent.get(1);

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

     or{
         ge("age", 3)

         ge("age", 5)
    }

}