2014-09-30 106 views
0

一直在挣扎着这一段时间。我所试图做的是建立在Grails的,我有父记录(的hasMany)标准的关系,这些记录包含两列(4,如果再算上ID和版本)Grails:保存孩子记录失败了已有的父记录

定义如下:

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) 

它倒了没有尽头的,我不是很了解的输出是诚实的。 (OK,前两行是我加的东西,看看是否变量,其中空

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 

而且我确实做到了这对同期增长检查续存在的数据库相同的检查。

EDIT 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 
+0

保存调用失败,因为数据验证失败,因为国家的impdate不能为空。 – sebnukem 2014-09-30 20:56:25

+0

至于将日期设置为空的步骤之后的第二个错误,请尝试重新启动您的应用,然后尝试'grails clean'并重新启动。 – sebnukem 2014-09-30 20:58:11

+0

这是我无法理解的,日期不为空,我把它称为行前。有一个快速检查,这绝对是正确的,但认为是。 – vrghost 2014-09-30 20:58:46

回答

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) 
} 
+0

Sebnukem:我有这样的支票,问题只是它不喜欢我提供的日期,但没有直接的错误信息。 – vrghost 2014-10-01 05:56:22

相关问题