我試圖用很多測試數據填充我的數據庫,所以我編寫了一個CommandLineRunner來保存2k個實體。彈簧引導jpa - 生成並保存測試數據
它的工作 - 但需要永久完成(5-10分鐘) - 我處理這個錯誤的方式?
@Component
public class DbSeederTest implements CommandLineRunner {
@Autowired
FirstRepo firstRepo;
@Autowired
SecondRepo secondRepo;
@Autowired
ThirdRepo thirdRepo;
private List<FirstEnt> firstList = new ArrayList<>();
private List<SecondEnt> secondList = new ArrayList<>();
private List<ThirdEnt> thirdList = new ArrayList<>();
private void generateTestData() {
// generate alot of entities, and add them to the Lists
}
@Override
public void run(String... args) throws Exception {
System.out.println("saving ents...");
generateTestData();
try {
firstRepo.save(firstList);
secondRepo.save(secondList);
thirdRepo.save(thirdList);
} catch(Exception e) {
e.printStackTrace();
}
}
}
我不知道爲什麼它花費了太多的時間,但你可以通過使用線程概念確實減少時間。由於數據已經在'generateTestData();'後面填充,所以現在在不同線程中調用每個repo save方法。 – SachinSarawgi