Grails多对多 - 找到所有不包含特定对象的对象 [英] Grails many-to-many - find all objects that do not contain specific object
本文介绍了Grails多对多 - 找到所有不包含特定对象的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下域模型:
class Product {
static hasMany = [certificates:Certificate]
class证书{
static hasMany = [产品:产品]
static belongsTo = [Product]
}
如何查找所有不含特定证书的产品?最好用条件查询。
解决方案
使用Burt建议的方法这里
你可以这样写下你的查询:
def p = new Product(message:A)
p。 addToCertificates(新证书(消息:1).save(flush:true))
p.addToCertificates(新证书(消息:2).save(flush:true))
p。 addToCertificates(新证书(消息: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 :true))
p.addToCertificates(new Cert证书(message:2)。save(flush:true))
p.save(flush:true)
$ b def cer = Certificate.findByMessage(3 )
Product.executeQuery(
'从产品p中选择p,其中:不在元素中的证书(p.certificates)',[证书:cer])
输出:
结果:[B,C]
I have following domain model:
class Product {
static hasMany = [ certificates : Certificate ]
}
class Certificate {
static hasMany = [ products : Product ]
static belongsTo = [ Product ]
}
How can I find all products that do not contain specific certificate? Preferably with criteria query.
解决方案
Using the approach that Burt suggested here
You can write your query like this:
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])
Output:
Result: [B, C]
这篇关于Grails多对多 - 找到所有不包含特定对象的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文