gpt4 book ai didi

grails - 使用数据条件的Grails条件查询

转载 作者:行者123 更新时间:2023-12-02 13:51:31 24 4
gpt4 key购买 nike

我有一个数据库表,使用 Vanilla GORM存储此Grails域类的数据:

class A {
String propOver // may be null
String propBase
}

我想创建一个搜索查询,如果它包含一个值,则针对 propOver属性进行搜索,否则针对 propBase属性进行搜索。或者,换句话说, propOver在存在时会覆盖 propBase

我需要类似以下伪代码的东西:
def results = A.createCriteria().list{
if propOver isn't null: // the heart of the problem
eq('propOver', search_input)
else
eq('propBase', search_input)
}

可能吗

请注意,一个(错误的)解决方案是创建一个存储 propOver ?: propBase值的第3个属性,但它违反了DRY原理,我宁愿避免修改数据库。

最佳答案

这样会吗?

A.createCriteria().list{
or {
eq 'propOver', search_input
and {
isNull 'propOver'
eq 'propBase', search_input
}
}
}

关于grails - 使用数据条件的Grails条件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27003276/

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