Skip to content

Commit 2bf39a5

Browse files
Update README.md
1 parent 35facb2 commit 2bf39a5

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,3 +3031,20 @@ Calling `fetchWithBooksByGenre()` works fine only that the following warning is
30313031

30323032
**Description:** This application is a sample of using `CompletableFuture` for batching inserts. This `CompletableFuture` uses an `Executor` that has the number of threads equal with the number of your computer cores. Usage is in Spring style.
30333033

3034+
----------------------------------------------------------------------------------------------------------------------
3035+
3036+
214. **[How To Optimize Batch Inserts of Parent-Child Relationships In MySQL And Batch Per Transaction](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertOrderBatchPerTransaction)**
3037+
3038+
**Description:** Let's suppose that we have a one-to-many relationship between `Author` and `Book` entities. When we save an author, we save his books as well thanks to cascading all/persist. We want to create a bunch of authors with books and save them in the database (e.g., a MySQL database) using the batch technique. By default, this will result in batching each author and the books per author (one batch for the author and one batch for the books, another batch for the author and another batch for the books, and so on). In order to batch authors and books, we need to **order inserts** as in this application.
3039+
3040+
Moreover, this example commits the database transaction after each batch excecution. This way we avoid long-running transactions and, in case of a failure, we rollback only the failed batch and don't lose the previous batches.
3041+
3042+
**Key points:**
3043+
- beside all setting specific to batching inserts in MySQL, we need to set up in `application.properties` the following property: `spring.jpa.properties.hibernate.order_inserts=true`
3044+
- in your DAO layer, commit the database transaction after each batch execution
3045+
3046+
**Example without ordered inserts:**\
3047+
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/HibernateSpringBootBatchInsertOrder/batch%20inserts%20including%20associations%20no%20order%20of%20inserts.png)
3048+
3049+
**Example with ordered inserts:**\
3050+
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/HibernateSpringBootBatchInsertOrder/batch%20inserts%20including%20associations%20ordered%20inserts.png)

0 commit comments

Comments
 (0)