Avatar ·

Mass data insertion via JpaRepository in Spring Boot and Kotlin

Hi everyone! I'm writing an application in Kotlin + Spring Boot + Hibernate, and I need to insert many records into a CockroachDB table. Currently, I'm doing this in a loop using saveAll():\n\n```kotlin\n@Repository\ninterface UserRepository : JpaRepository\n\n@Service\nclass UserService(private val repo: UserRepository) {\n fun insertUsers(users: List) {\n users.forEach { repo.save(it) } // or repo.saveAll(users)\n }\n}\n```\n\nProblem: if there are 1000+ users, this works very slowly. In the logs, I can see that Hibernate creates a separate INSERT for each record. I've read that batch inserts are possible, but I don't understand how to configure it properly.\n\nQuestions:\n1. How to do mass insertion via JpaRepository so that Hibernate combines INSERTs into one batch?\n2. Do I need to change ap