How to persist a lot of entities (JPA)

后端 未结 4 1433
半阙折子戏
半阙折子戏 2020-12-16 12:44

I need to process a CSV file and for each record (line) persist an entity. Right now, I do it this way:

while ((line = reader.readNext()) != null) {
    Enti         


        
4条回答
  •  没有蜡笔的小新
    2020-12-16 13:39

    To make it go faster, at least in Hibernate, you would do a flush() and a clear() after a certain number of inserts. I have done this approach for millions of records and it works. It's still slow, but it's much faster than not doing it. The basic structure is like this:

    int i = 0;
    for(MyThingy thingy : lotsOfThingies) {
    
        dao.save(thingy.toModel())
    
        if(++i % 20 == 0) {
            dao.flushAndClear();
        }
    
    }
    

提交回复
热议问题