Domain Class:
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')
}
Hi, I've posted above all the necessary codes that might help you analyze what the problem was. Basically, the error is syncFlag
and dateLastModified
cannot be null. So what I did was removed these keys by using
params.remove('<column name>')
so that in the insert statement of hibernate these will be skipped and since I defined in MySQL that these should not be null, it will use the default value set. The problem is that I kept getting that same error (.... cannot be null). So what I did was set the default value of these in the domain class but it didn't works either. I also tried to hard code it like this:
params.syncFlag=false
but it also didn't works.
When print the value of those 2 fields, I get null even if I hard coded it already.
I think I've already made everything that I can to debug but all of it failed. Could you tell me what might have caused this and why I'm getting cannot be null?
Your domain class is set to not allow null properties. That's the Grails default. To allow nulls, use the nullable constraint .
class Employee {
...
static constraints = {
dateLastModified nullable: true
syncFlag nullable: true
}
}
To get a Map without certain keys you can follow this example:
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"]
You can filter them out with findAll() . Note that by filter I mean the original params remains unchanged.
You might actually think that this is weird but this fixed my problem.
Employee employeeInstance = new Employee(params);
employeeInstance.setSyncFlag(false);
employeeInstance.setDateLastModified(new Date());
employeeInstance.save(flush:true)
The issue is I had the hard time changing some of the value in params, and i dont know why. So what I did was, before I call save, I changed the value using the setters. And that worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.