gpt4 book ai didi

hibernate - Grails:无法设置参数的值,并且获取时不能为null

转载 作者:行者123 更新时间:2023-12-02 15:23:20 25 4
gpt4 key购买 nike

域类:

class Employee {
.
.
Boolean syncFlag
Date dateLastModified
}

MySQL:
Column name              Data type                    Default value
---------------------------------------------------------------------
sync_flag BIT(1) Not Null b'0'

date_last_modified TIMESTAMP Not Null CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Controller :
def save() {      
.
.

params.remove('id')

if(params.dateLastModified){
params.remove('dateLastModified')
}

if(params.syncFlag){
params.remove('syncFlag')
}

嗨,我已经发布了所有必要的代码,它们可以帮助您分析问题所在。基本上,错误是 syncFlag,并且 dateLastModified不能为null。所以我所做的是通过使用删除了这些键
params.remove('<column name>')

因此,在hibernate的insert语句中,这些将被跳过,并且由于我在MySQL中定义了这些不应为null,因此它将使用默认值集。问题是我一直收到相同的错误(..不能为空)。因此,我在域类中设置了这些的默认值,但是它也不起作用。我也尝试像这样硬编码:
params.syncFlag=false

但它也不起作用。

当打印这两个字段的值时,即使我已经对其进行了硬编码,我也将获得null。

我想我已经做了所有可以调试的工作,但都失败了。您能告诉我是什么原因造成的,为什么我不能为空?

最佳答案

您的域类设置为不允许使用null属性。那是Grails的默认设置。要允许空值,请使用nullable constraint

class Employee {
...
static constraints = {
dateLastModified nullable: true
syncFlag nullable: true
}
}

暗示

要获取没有某些键的 map ,您可以按照以下示例操作:
def params = [
id: 100,
dateLastModified: new Date(),
syncFlag: true,
other: "This won't get filtered out"]

def ignore = ['id', 'dateLastModified', 'syncFlag']
assert params.findAll { key, value -> !(key in ignore) } == [other:"This won't get filtered out"]

您可以使用findAll()过滤掉它们。请注意,过滤器是指原始参数保持不变。

关于hibernate - Grails:无法设置参数的值,并且获取时不能为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32093175/

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