Grails:对于已经存在的父记录,保存子记录失败
我已经为此挣扎了一段时间了。我试图做的是在grails中创建一个标准关系,其中我有父记录(hasMany),这些记录包含两列(如果我们计算id和版本,则为4列) 定义如下:Grails:对于已经存在的父记录,保存子记录失败,grails,Grails,我已经为此挣扎了一段时间了。我试图做的是在grails中创建一个标准关系,其中我有父记录(hasMany),这些记录包含两列(如果我们计算id和版本,则为4列) 定义如下: class Country { static hasMany = [regstats: Regstat] String countryiso String countryname static constraints = { countryiso size:2..2,
class Country {
static hasMany = [regstats: Regstat]
String countryiso
String countryname
static constraints = {
countryiso size:2..2, unique: true, validator:{ it.toUpperCase() == it }
}
static mapping = {
index: 'countryiso'
}
}
此表非常简单,它包含每个国家的iso3611代码及其全名
第二个包含一组关于这个国家的数据,我希望它有孩子/父母的关系。也就是说,每个国家可以有多个记录,但在任何时候都只能存在一个唯一的国家/地区类型
class Regstat {
static belongsTo = [country: Country]
String reg
int status
String exturl
Date impdate
Date lupdate
String impnote
static constraints = {
reg(inList: ["FATCA", "ITC2014", "AEOI"], unique:'country')
exturl(nullable:true)
impnote(nullable:true)
}
static mapping = {
index: 'reg'
impnote type: 'text'
}
}
我有一个函数,将从csv文件加载国家定义,所有良好,创建所有243个国家
然后我尝试创建子记录(在另一个函数中,国家已保存到数据库中,已检查)
运行以下命令:
render ("Writing some stuff<BR>")
render ("To be more precise: ${exturl} ${impdate} ${lupdate} ${impnote} ${ref} ${status} <BR>")
def qp = Country.findByCountryname(cont)
qp.addToRegstats(new Regstat(exturl:exturl, impdate:impdate, lupdate:lupdate,impnote:impnote,reg:ref,status:status))
qp.save(flush:true,failOnError:true)
我之前对数据库做了完全相同的检查,以检查cont是否存在
编辑2
更改了渲染线,以便人们可以看到每个变量中的内容:
收到此消息(当impdate、impnote、exturl和lupdate都可为空时)
在添加子记录之前,请检查是否已找到父实例:
qp = Country.findByCountryname(cont)
if (qp) {
qp.addToRegstats(new Regstat(exturl:exturl, impdate:impdate, update:lupdate, impnote:impnote, reg:ref, status:status)).save(flush:true,failOnError:true)
}
save调用失败,因为数据验证失败,因为国家/地区的impdate不能为空。对于将日期设置为可空的步骤后的第二个错误,请尝试重新启动应用程序,然后尝试
grails clean
并重新启动。这是我无法理解的,日期不是空的,我之前就这么说了。我将快速查看以检查是否确实是正确的,但我认为是。然后,您确定它是日期类型而不是字符串吗?我已经尝试了清除,现在所有日期都设置为可空,并且我之前在行上输出了数据(添加为edit two),但仍然没有运气。数据库里什么也没有。(这是一个mysql数据库,如果有区别的话)Sebnukem:我做过这样的检查,问题是它不喜欢我提供的日期,但没有直接的错误消息。
/RegMap/countryupload/loadcsv
Class
java.lang.NullPointerException
Message
null
Writing some stuff
To be more precise: exturl:N/A impdate:2014-06-30 update:2014-09-29 impdate: ref:FATCA status:3
Error 500: Internal Server Error
URI
/RegMap/countryupload/loadcsv
Class
java.lang.NullPointerException
Message
null
qp = Country.findByCountryname(cont)
if (qp) {
qp.addToRegstats(new Regstat(exturl:exturl, impdate:impdate, update:lupdate, impnote:impnote, reg:ref, status:status)).save(flush:true,failOnError:true)
}