Grails域级实现saveAll

时间:2017-03-03 07:46:01

标签: csv grails bulkinsert

我有三个域类

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

其他是

   class CallCallerList {
    String reason
    Caller caller 
    CallerList callerList
}
class CallerList {

    String name

}
  

呼叫者

  

CallerList

有一对多的关系
  

CallCallerList

我有一个巨大的csv文件,我在那里读取所有调用者并将它们放入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时发生了一些错误,我的进程会变慢,那么所有调用者都会保存,但不会保存在任何调用者列表中。 我想这样做

Caller.saveAll(callers)
CallCallerList(callCallerLists)

1 个答案:

答案 0 :(得分:0)

我已经这样做并解决了上述问题。

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)