我有三个 Realm 类(class)

   class Caller implements Serializable{
    String callingNumber
    String callerName
    static hasMany = [ callCallerList : CallCallerList ]
}

其他是
   class CallCallerList {
    String reason
    Caller caller
    CallerList callerList
}
class CallerList {

    String name

}







与一对多关系



我有一个巨大的csv文件,我在其中读取所有 call 者并将其放入callCallerList中。
    def callerList = new CallerList(name:'test')
    def callCallerLists = []
    callerList.save(flush:true)
     groovyFile.each {
          ArrayList<String> line = it.split(',').toList()
          def caller = new Caller(callingNumber:line.get(0),callerName:line.get(1))
//I want to save the object latter when I run the saveAll function for this domain class.
def callCallerList = new CallCallerList(caller:caller,callerList:callerList,reason:line.get(2))
    callCallerLists.add(callCallerList)
// this gives me the error that caller is unsaved object.
}
CallCallerList.saveAll(callCallerLists)

我不想保存调用者,因为如果文件中有数百万条记录,并且在创建批量callCallerList时发生了某些错误,则我的过程会变慢,那么将保存所有调用者,但不会保存在任何callerList中。
我想做这个
Caller.saveAll(callers)
CallCallerList(callCallerLists)

最佳答案

我已经做到了,并解决了上述问题。

def callerList = new CallerList(name:'test')
    def callCallerLists = []
    def callers = []
    callerList.save(flush:true)
     groovyFile.each {
          ArrayList<String> line = it.split(',').toList()
          def caller = new Caller(callingNumber:line.get(0),callerName:line.get(1))
//I want to save the object latter when I run the saveAll function for this domain class.
def callCallerList = new CallCallerList(callerList:callerList,reason:line.get(2))
     callers.add(caller)
    callCallerLists.add(callCallerList)
// this gives me the error that caller is unsaved object.
}
Caller.saveAll(callers)
for(int i =0;i< callCallerLists?.size();i++){
                    callCallerLists?.get(i)?.caller = callers?.get(i)
                }
CallCallerList.saveAll(callCallerLists)

10-06 05:10