0

以下是我的域类

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 返回发送者对象的预期结果。但是当我访问发送者对象以获取相应的接收者时,它会再次从数据库中加载所有接收者。

有没有办法解决这个问题。

4

1 回答 1

1

您可以在创建别名时指定连接类型。

    createAlias("receivers", "t", org.hibernate.criterion.CriteriaSpecification.LEFT_JOIN)
于 2012-09-26T04:54:42.170 回答