akiong akiong - 29 days ago 10
Groovy Question

how to get data with findAllBy with join table condition

i am confuse to retrive data from 2 a table where

belong to
other table.

like this..
this is domain
TransaksiKas.groovy


class TransaksiKas {
String trxNo
Date trxDate
String ctrlNo
Kas kas
Cif cif
String deleteFlag = "N"

static constraints = {

}
}


and this is
TransaksiKasDetail.groovy


class TransaksiKasDetail {
TransaksiKas transaksiKas
Account account
Invoice salesInvoice
PurchaseInvoice purchaseInvoice
BigDecimal amount
String dt //D atau K
String keterangan

String cekNo
String cekNoAvailable
Date cekDueDate
Bank bank

static constraints = {
amount(nullable: false, blank: true, min: new BigDecimal('0.0'), max: new BigDecimal('99999999999999999.99'), scale: 2)
account(nullable: true,blank:true)
purchaseInvoice(nullable: true,blank: true)
salesInvoice(nullable: true,blank: true)
dt(nullable: true,blank: true)
keterangan(nullable: true,blank: true)
cekNo(nullable: true,blank: true)
cekDueDate(nullable: true,blank:true)
bank(nullable: true,blank:true)
cekNoAvailable(nullable: true,blank:true)
}


}

in my case, i am trying to get all data from
TransaksiKasDetail
where
TransaksiKasDetail.transaksiKas
has
cif
=
1
.

i tried like this..

def cekList = TransaksiKasDetail.findAllByAndCekNoAvailableAndTransaksiKas("N",trx)


in this case,
i need to get all data from
TransaksiKasDetail
where has
transaksiKas
with
cif = 2
and
cekDuedate
=
today

Answer

you can use findAll

for example.

 def example = TransaksiKasDetail.findAll(
            "from TransaksiKasDetail as u  where u.cekNoAvailable = :cek and  cekNo != null and cekDueDate = :skrg and transaksiKas.cif = :cif" , [cif:cif,cek:'N',skrg:new Date().clearTime()])

using findAll, you can create a condition in TransaksiKas

for more information, you can read this