Ask Your Question
1

What is the method for performing bulk (multiple row) inserts with JpaRepository?

asked 2023-07-04 14:28:08 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-07-04 14:53:02 +0000

pufferfish gravatar image

The JpaRepository interface provides a saveAll() method to perform bulk inserts of multiple entities. The method accepts an iterable of entities and inserts them all into the database in a single transaction.

Here's an example:

//Assuming entity name is Product
List<Product> productList = new ArrayList<>();
productList.add(new Product("Product1", 10));
productList.add(new Product("Product2", 20));
productList.add(new Product("Product3", 30));

productRepository.saveAll(productList);

This will insert all three Product entities into the database in a single transaction.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-04 14:28:08 +0000

Seen: 9 times

Last updated: Jul 04 '23