Grails:对于已存在的父记录,保存子记录失败

时间:2014-09-30 20:37:19

标签: grails

现在已经挣扎了一段时间。我想要做的是在grails中创建一个标准关系,我有父记录(hasMany),这些记录包含两列(如果我们计算id和版本,则为4)

定义如下:

    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代码及其全名。

第二个包含有关该国家/地区的大量数据,我希望它具有子/父关系。 IE,每个国家/地区可以有多个记录,但任何时候都只能存在一个唯一的国家/地区类型。

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)

它完全没有尽头,但我不能完全理解输出。 (好吧,前两行是我添加一些内容,看看变量是否为空。

Writing some stuff
To be more precise: N/A 2014-06-30 2014-09-29 FATCA 3 
Error 500: Internal Server Error
URI
/RegMap/countryupload/loadcsv
Class
grails.validation.ValidationException
Message
Validation Error(s) occurred during save(): - Field error in object 'regmap.Country' on field 'regstats[0].impdate': rejected value [null]; codes [regmap.Regstat.impdate.nullable.error.regmap.Country.regstats[0].impdate,regmap.Regstat.impdate.nullable.error.regmap.Country.regstats.impdate,regmap.Regstat.impdate.nullable.error.regstats[0].impdate,regmap.Regstat.impdate.nullable.error.regstats.impdate,regmap.Regstat.impdate.nullable.error.impdate,regmap.Regstat.impdate.nullable.error.java.util.Date,regmap.Regstat.impdate.nullable.error,regstat.impdate.nullable.error.regmap.Country.regstats[0].impdate,regstat.impdate.nullable.error.regmap.Country.regstats.impdate,regstat.impdate.nullable.error.regstats[0].impdate,regstat.impdate.nullable.error.regstats.impdate,regstat.impdate.nullable.error.impdate,regstat.impdate.nullable.error.java.util.Date,regstat.impdate.nullable.error,regmap.Regstat.impdate.nullable.regmap.Country.regstats[0].impdate,regmap.Regstat.impdate.nullable.regmap.Country.regstats.impdate,regmap.Regstat.impdate.nullable.regstats[0].impdate,regmap.Regstat.impdate.nullable.regstats.impdate,regmap.Regstat.impdate.nullable.impdate,regmap.Regstat.impdate.nullable.java.util.Date,regmap.Regstat.impdate.nullable,regstat.impdate.nullable.regmap.Country.regstats[0].impdate,regstat.impdate.nullable.regmap.Country.regstats.impdate,regstat.impdate.nullable.regstats[0].impdate,regstat.impdate.nullable.regstats.impdate,regstat.impdate.nullable.impdate,regstat.impdate.nullable.java.util.Date,regstat.impdate.nullable,nullable.regmap.Country.regstats[0].impdate,nullable.regmap.Country.regstats.impdate,nullable.regstats[0].impdate,nullable.regstats.impdate,nullable.impdate,nullable.java.util.Date,nullable]; arguments [impdate,class regmap.Regstat]; default message [Property [{0}] of class [{1}] cannot be null] - Field error in object 'regmap.Country' on field 'regstats[0].lupdate': rejected value [null]; codes [regmap.Regstat.lupdate.nullable.error.regmap.Country.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.error.regmap.Country.regstats.lupdate,regmap.Regstat.lupdate.nullable.error.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.error.regstats.lupdate,regmap.Regstat.lupdate.nullable.error.lupdate,regmap.Regstat.lupdate.nullable.error.java.util.Date,regmap.Regstat.lupdate.nullable.error,regstat.lupdate.nullable.error.regmap.Country.regstats[0].lupdate,regstat.lupdate.nullable.error.regmap.Country.regstats.lupdate,regstat.lupdate.nullable.error.regstats[0].lupdate,regstat.lupdate.nullable.error.regstats.lupdate,regstat.lupdate.nullable.error.lupdate,regstat.lupdate.nullable.error.java.util.Date,regstat.lupdate.nullable.error,regmap.Regstat.lupdate.nullable.regmap.Country.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.regmap.Country.regstats.lupdate,regmap.Regstat.lupdate.nullable.regstats[0].lupdate,regmap.Regstat.lupdate.nullable.regstats.lupdate,regmap.Regstat.lupdate.nullable.lupdate,regmap.Regstat.lupdate.nullable.java.util.Date,regmap.Regstat.lupdate.nullable,regstat.lupdate.nullable.regmap.Country.regstats[0].lupdate,regstat.lupdate.nullable.regmap.Country.regstats.lupdate,regstat.lupdate.nullable.regstats[0].lupdate,regstat.lupdate.nullable.regstats.lupdate,regstat.lupdate.nullable.lupdate,regstat.lupdate.nullable.java.util.Date,regstat.lupdate.nullable,nullable.regmap.Country.regstats[0].lupdate,nullable.regmap.Country.regstats.lupdate,nullable.regstats[0].lupdate,nullable.regstats.lupdate,nullable.lupdate,nullable.java.util.Date,nullable]; arguments [lupdate,class regmap.Regstat]; default message [Property [{0}] of class [{1}] cannot be null]
Around line 66 of grails-app/controllers/regmap/CountryuploadController.groovy
63:                     render ("To be more precise: ${exturl} ${impdate} ${lupdate} ${impnote} ${ref} ${status} <BR>")
64:                     def qp = Country.findByCountryname(cont)
65:                         qp.addToRegstats(new Regstat(exturl:exturl, impdate:impdate, lupdate:lupdate,impnote:impnote,reg:ref,status:status))
66:                         qp.save(flush:true,failOnError:true)

更新 为了它的乐趣,我将两个日期设置为可以为空,然后我得到了可能最少有用的错误消息 URI

/RegMap/countryupload/loadcsv
Class
java.lang.NullPointerException
Message
null

我之前对数据库做了完全相同的检查以检查是否存在。

编辑2 更改了渲染线,以便人们可以看到每个变量中的内容: 得到这条消息(当impdate,impnote,exturl和lupdate都可以为空时)

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 

1 个答案:

答案 0 :(得分:1)

在添加子记录之前检查是否找到了父实例:

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)
}
相关问题