gpt4 book ai didi

grails - 多对多Grails-查找不包含特定对象的所有对象

转载 作者:行者123 更新时间:2023-12-02 14:34:37 26 4
gpt4 key购买 nike

我有以下域模型:

class Product {
static hasMany = [ certificates : Certificate ]
}

class Certificate {
static hasMany = [ products : Product ]
static belongsTo = [ Product ]
}

如何找到所有不包含特定证书的产品?最好使用标准查询。

最佳答案

使用Burt建议的方法here

您可以这样编写查询:

 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]

关于grails - 多对多Grails-查找不包含特定对象的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17074841/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com