与Grails中的hasMany关系不同的结果

时间:2014-07-02 11:14:37

标签: grails gorm

我有两个类,即UserSurvey和SurveyResponseSet。 UserSurvey有许多SurveyResponseSet。

class UserSurvey {
    Survey survey
    List responses = []
    String surveyCompleted = ''
    .
    .
    .   
    static hasMany = [
        responses: SurveyResponseSet
    ]

    static mapping = {
        responses cascade: 'delete'
    }
}


class SurveyResponseSet {
    UserSurvey userSurvey
    Integer sumWeight = 0
    .
    .
    .
    static constraints = {
        sumWeight nullable:true, default: 0
    }
}

要获取在UserSurveyObject.responses之前我正在进行的SurveyResponseSet列表,它一直运行到现在为止。今天它在迭代列表时开始抛出错误。 我使用的是Grails 2.4.0。我试过干净,CleanAll但它没有解决。 我进一步观察并发现UserSurveyObject.responses在列表中给出了一些空的额外结果。这是控制台的结果:

UserSurvey uSurvey = UserSurvey.get(1437);
List<SurveyResponseSet> listFromSurvey = uSurvey.responses;
List<SurveyResponseSet> listUsingSurvey = SurveyResponseSet.findAllByUserSurvey(uSurvey); 

println listFromSurvey; 
println listUsingSurvey;

[null, nexant.SurveyResponseSet : 9788, nexant.SurveyResponseSet : 9789, nexant.SurveyResponseSet : 9793, nexant.SurveyResponseSet : 9794, nexant.SurveyResponseSet : 9795, null, nexant.SurveyResponseSet : 9874, nexant.SurveyResponseSet : 9875, nexant.SurveyResponseSet : 9879, nexant.SurveyResponseSet : 9880, nexant.SurveyResponseSet : 9881, nexant.SurveyResponseSet : 9882, null, nexant.SurveyResponseSet : 9884, nexant.SurveyResponseSet : 9885]


[nexant.SurveyResponseSet : 9795, nexant.SurveyResponseSet : 9794, nexant.SurveyResponseSet : 9793, nexant.SurveyResponseSet : 9789, nexant.SurveyResponseSet : 9788, nexant.SurveyResponseSet : 9885, nexant.SurveyResponseSet : 9884, nexant.SurveyResponseSet : 9882, nexant.SurveyResponseSet : 9881, nexant.SurveyResponseSet : 9880, nexant.SurveyResponseSet : 9879, nexant.SurveyResponseSet : 9875, nexant.SurveyResponseSet : 9874]

我不知道从哪里获取了三个空的额外对象。

1 个答案:

答案 0 :(得分:0)

关于null你可以试试:

class SurveyResponseSet {
    UserSurvey userSurvey
    Integer sumWeight = 0
    .
    .
    .
     static mapping = { 
       userSurvey ignoreNotFound : true
     }

    static constraints = {
        sumWeight nullable:true, default: 0
    }
}

看看是否从列表中删除了空元素