我有一个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位(关联),是否可能? 。或者我的域类设计错了?
答案 0 :(得分:1)
我认为这应该有效:
def results = Bid.withCriteria {
order {
eq 'id', 1
}
projections {
property 'order'
}
maxResults 10
}
但请注意,您必须更改Bid
域类,以便以Bid
到Order
的其他方式添加关系:
class Bid {
...
static belongsTo = [order: Order]
}