|
64 | 64 |
|
65 | 65 | -----------------------------------------------------------------------------------------------------------------------
|
66 | 66 |
|
67 |
| -5. **[How To Batch Inserts Via EntityManager In MySQL](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertsEntityManager)** |
| 67 | +5. **[Batch Inserts Via EntityManager in MySQL](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertsEntityManager)** |
68 | 68 |
|
69 | 69 | **Description:** Batch inserts via `EntityManager` in MySQL. This way you can easily control the `flush()` and `clear()` of the persistence context (1st level cache). This is not possible via SpringBoot, `saveAll(Iterable<S> entities)`. Another advantage is that you can call `persist()` instead of `merge()` - this is used behind the scene by the SpringBoot `saveAll(Iterable<S> entities)` and `save(S entity)`.
|
70 | 70 |
|
71 | 71 | **Key points:**\
|
72 |
| - - in application.properties set `spring.jpa.properties.hibernate.jdbc.batch_size`\ |
73 |
| - - in application.properties set `spring.jpa.properties.hibernate.generate_statistics` (just to check that batching is working)\ |
74 |
| - - in application.properties set JDBC URL with `rewriteBatchedStatements=true` (optimization for MySQL)\ |
75 |
| - - in application.properties set JDBC URL with `cachePrepStmts=true` (enable caching and is useful if you decide to set `prepStmtCacheSize`, `prepStmtCacheSqlLimit`, etc as well; without this setting the cache is disabled)\ |
76 |
| - - in application.properties set JDBC URL with `useServerPrepStmts=true` (this way you switch to server-side prepared statements (may lead to signnificant performance boost))\ |
| 72 | + - in `application.properties` set `spring.jpa.properties.hibernate.jdbc.batch_size`\ |
| 73 | + - in `application.properties` set `spring.jpa.properties.hibernate.generate_statistics` (just to check that batching is working)\ |
| 74 | + - in `application.properties` set JDBC URL with `rewriteBatchedStatements=true` (optimization for MySQL)\ |
| 75 | + - in `application.properties` set JDBC URL with `cachePrepStmts=true` (enable caching and is useful if you decide to set `prepStmtCacheSize`, `prepStmtCacheSqlLimit`, etc as well; without this setting the cache is disabled)\ |
| 76 | + - in `application.properties` set JDBC URL with `useServerPrepStmts=true` (this way you switch to server-side prepared statements (may lead to signnificant performance boost))\ |
77 | 77 | - in case of using a parent-child relationship with cascade persist (e.g. one-to-many, many-to-many) then consider to set up `spring.jpa.properties.hibernate.order_inserts=true` to optimize the batching by ordering inserts\
|
78 | 78 | - in entity, use the [assigned generator](https://vladmihalcea.com/how-to-combine-the-hibernate-assigned-generator-with-a-sequence-or-an-identity-column/) since MySQL `IDENTITY` will cause batching to be disabled\
|
79 |
| - - in DAO, flush and clear the persistence context from time to time. This way you avoid to "overwhelm" the persistence context. |
| 79 | + - in DAO, flush and clear the persistence context from time to time; this way you avoid to "overwhelm" the persistence context\ |
| 80 | + - if is not needed then ensure that Second Level Cache is disabled via `spring.jpa.properties.hibernate.cache.use_second_level_cache=false` |
80 | 81 |
|
81 | 82 | **Output example:**
|
82 |
| - |
| 83 | + |
83 | 84 |
|
84 | 85 | -----------------------------------------------------------------------------------------------------------------------
|
85 | 86 |
|
|
0 commit comments