如何限制grails中的关联大小?

时间:2014-06-07 18:12:29

标签: grails gorm grails-2.0

我有一个grails域类,如下所示:

 class Order {
   String orderId = 'OD' + System.nanoTime().toString()
   Date orderedDate
   String itemName
   List bids;
   static hasMany = [ bids: Bid ;likedUsers: User,]
   static belongsTo =[owner:User]
  }

 class Bid {
  Integer amount
  User bidedUser
     static belongsTo = [Order]
  }
   class User {
     String username
     String password
     String emailId
     List orders
     static hasMany = [orders:Order]
     }

我要做的是,查询包含maxResult为10的位的订单,如

 def critObj = Order.createCriteria() 
     critObj.list{
       eq("id" ,1)
       bids {
         maxResult(10) //Trying to fetch only 10 records
        }
    }

如何只加载10位(关联),是否可能? 。或者我的域类设计错了?

1 个答案:

答案 0 :(得分:1)

我认为这应该有效:

def results = Bid.withCriteria {
    order {
        eq 'id', 1
    }
    projections {
        property 'order'
    }
    maxResults 10
}

但请注意,您必须更改Bid域类,以便以BidOrder的其他方式添加关系:

class Bid {
    ...
    static belongsTo = [order: Order]
}