如何限制grails中关联的大小? [英] How to limit the size of association in grails?
本文介绍了如何限制grails中关联的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
class Order {
String orderId ='OD'+ System.nanoTime()。toString()
Date orderedDate
String itemName
列出出价;
static hasMany = [bid:Bid; likedUsers:User,]
static belongsTo = [owner:User]
}
class Bid {
Integer金额
用户bidedUser
static belongsTo = [订单]
}
类用户{
字符串用户名
字符串密码
字符串emailId
列出订单
static hasMany = [订单:订单]
}
什么我试图做的是,以maxResult为10的位查询订单,就像
def critObj = Order.createCriteria ()
critObj.list {
eq(id,1)
bids {
maxResult(10)//试着只抓取10条记录
}
如何只加载10位(关联),有可能吗? 。或者我的领域类的设计是错误的?
解决方案
我认为这应该工作:
def results = Bid.withCriteria {
order {
eq'id',1
}
预测{
$'$'
$ b $ >但请注意,您必须更改您的出价
域类,以便以另一种方式从出价
添加关系到订单
:
class出价{
...
static belongsTo = [order:Order]
}
I have a grails domain class as below :
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]
}
What I am trying to do is , to query for an order with bits with maxResult of 10 as like
def critObj = Order.createCriteria()
critObj.list{
eq("id" ,1)
bids {
maxResult(10) //Trying to fetch only 10 records
}
}
How can I load only 10 bits(associations) , is it possible? . Or My design of domain class is wrong?
解决方案 I think this should work:
def results = Bid.withCriteria {
order {
eq 'id', 1
}
projections {
property 'order'
}
maxResults 10
}
But please note that you have to change your Bid
domain class to add the relation in the other way from Bid
to Order
:
class Bid {
...
static belongsTo = [order: Order]
}
这篇关于如何限制grails中关联的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文