[英]Grails many-to-many - find all objects that contain specific list of objects
[英]Grails many-to-many - find all objects that do not contain specific object
我有以下域模型:
class Product {
static hasMany = [ certificates : Certificate ]
}
class Certificate {
static hasMany = [ products : Product ]
static belongsTo = [ Product ]
}
如何找到所有不包含特定證書的產品? 最好使用標准查詢。
使用伯特建議的方法在這里
您可以這樣編寫查詢:
def p = new Product(message:"A")
p.addToCertificates (new Certificate(message:"1").save(flush:true) )
p.addToCertificates (new Certificate(message:"2").save(flush:true) )
p.addToCertificates (new Certificate(message:"3").save(flush:true) )
p.save(flush:true)
p = new Product(message:"B")
p.addToCertificates (new Certificate(message:"1").save(flush:true) )
p.addToCertificates (new Certificate(message:"2").save(flush:true) )
p.save(flush:true)
p = new Product(message:"C")
p.addToCertificates (new Certificate(message:"1").save(flush:true) )
p.addToCertificates (new Certificate(message:"2").save(flush:true) )
p.save(flush:true)
def cer= Certificate.findByMessage("3")
Product.executeQuery(
'select p from Product p where :certificate not in elements(p.certificates)',[certificate: cer])
輸出:
結果:[B,C]
嘗試這個:
Certificate certificate = Certificate.findTheSpecificOne()
def c = Product.createCriteria()
def results = c.list {
createAlias("certificates", "c", CriteriaSpecification.LEFT_JOIN)
not {'in'("c", certificate)}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.