gpt4 book ai didi

grails - 如何使用GORM验证字段是否为空字符串?

转载 作者:行者123 更新时间:2023-12-02 14:41:36 24 4
gpt4 key购买 nike

我有一个GORM查询,我需要使其避免使用一个字段,该字段可能是遗留系统问题的空字符串。

目前,我的查询是:

def things = Thing.withCriteria{
resultTransformer CriteriaSpecification.ALIAS_TO_ENTITY_MAP
createAlias 'factor', 'f'
eq 'active', true
isNotNull 'title' // this field may be an empty string
isNotNull 'content' // this field also may be an empty string
// <-- and here I'd like to include sth like isEmpty 'title'
not {
inList 'f.id', factors
}
projections{
property 'id' , 'id'
property 'f.name' , 'name'
property 'title' , 'title'
property 'content' , 'content'
property 'f.id' , 'factorId'
}
}

我没有在文档中找到任何解决方案,但我有一种感觉,即该解决方案应使用纯SQL。可能吗?有什么建议吗?

最佳答案

您是否尝试过将此语句添加到条件中?

ne title, ""

Grails条件依赖于 hibernate Restrictions,因此它所支持的所有内容也可以在grails条件中使用。

否则,您也可以使用 sql restrictions (see Using SQL Restrictions section):
sqlRestriction "char_length(title) > 0"

请记住,此处的 title指数据库中的实际列名。如果您想要一个更可靠的代码,则可以在模型中包括字段(列名)的静态映射:
static mapping = {
title column: 'title'
}

这样,您应该避免数据库特定命名行为(如果有)引起的错误。

关于grails - 如何使用GORM验证字段是否为空字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32402474/

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