【问题标题】:Grails: Saving a child record fails for already existing parent recordGrails:为已经存在的父记录保存子记录失败
【发布时间】:2014-09-30 20:37:18
【问题描述】:

已经为此苦苦挣扎了一段时间。我想做的是在我有父记录(hasMany)的grails中创建一个标准关系,这些记录包含两列(如果我们计算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)

更新: 为了好玩,我也将两个日期都设置为可空,然后我收到了可能是有史以来最没有帮助的错误消息 网址

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

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

编辑 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 

【问题讨论】:

  • 保存调用失败,因为数据验证失败,因为 Country 的 impdate 不能为空。
  • 至于将日期设置为可空的步骤之后的第二个错误,请尝试重新启动您的应用,然后尝试grails clean 并重新启动。
  • 这是我看不懂的,日期不为空,我之前叫它行。会快速查看一下这绝对是正确的,但认为它是正确的。
  • 那么,你确定是Date类型而不是字符串?
  • 已经尝试干净,现在所有日期都设置为可空,并且我在之前的行输出数据(添加为编辑二),仍然没有运气。并且数据库中没有任何内容。 (如果有区别,这是一个 mysql 数据库)

标签: grails


【解决方案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)
}

【讨论】:

  • Sebnukem:我有一张这样的支票,问题只是它不喜欢我提供的日期,但没有直接的错误消息。
猜你喜欢
  • 2013-01-28
  • 2011-04-04
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多