以下是我的域类
class Sender {
String senderFname;
String senderLname;
String senderPhone;
Collection receivers
static hasMany = [receivers: Receiver]
}
class Receiver {
String receiverFname;
String receiverLname;
String receiverPhone;
static belongsTo = [sender: Sender]
}
我正在编写简单搜索功能的逻辑。
def customers() {
println 'customers params '+params
//def senderCriteria = Sender.createCriteria()
def senderList = Sender.withCriteria {
if(params.senderfname)
like('senderFname',params.senderfname)
if(params.senderlname)
like('senderLname',params.senderlname)
if(params.senderphone)
like('senderPhone',params.senderphone)
createAlias("receivers", "t")
//receivers {
if(params.receiverfname)
like('t.receiverFname',params.receiverfname)
if(params.recieverlname)
like('t.receiverLname',params.receiverlname)
if(params.recieverphone)
like('t.receiverPhone',params.receiverphone)
//}
}
println "senderList "+senderList;
senderList.each{ it-> println 'sender '+it
println 'receivers '+it.receivers
}
//senderList = senderList.unique()
println "senderList "+senderList;
render(view: 'customers', model: [senderList: senderList])
}
发件人与收件人之间存在一对多的关系。我想过滤发件人对象及其相应的接收器对象。
主createCriteria返回发件人对象的预期结果。但是当我访问sender对象以获取相应的接收器时,它会再次从数据库加载所有接收器。
有没有办法解决这个问题。
答案 0 :(得分:1)
您可以在createAlias时指定连接类型。
createAlias("receivers", "t", org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN)