csv - Grails域级别的saveAll实现

标签 csv grails bulkinsert

我有三个 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

}

Caller





CallerList



与一对多关系

CallCallerList



我有一个巨大的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)

关于csv - Grails域级别的saveAll实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42573337/

相关文章:

python - 用 pandas 解析 CSV 并转换为 dict 结果仅在最后一行出现 KeyError

javascript - 数组数据未使用 this.props 在数据表中呈现 - ReactJS

validation - Grails命令对象验证给出异常而不是错误

mysql - MySQL 多行插入是否获取连续的自动增量 ID?

ios - 在sqlite中是否必须使用“END TRANSACTION”

python - 如何在python中检查上传的文件是csv还是xls?

mysql - 导入 CSV 到 MySQL

grails - 无法解析类org.apache.commons.configuration.PropertiesConfiguration

java - Tomcat 7.0.40 在失效后返回相同的 session ID

java - 当批量大时,JDBC 应用程序挂起进行批量插入