Skip to content

Commit aff4978

Browse files
Batch inserts via EntityManager
1 parent 9defdac commit aff4978

File tree

1 file changed

+8
-7
lines changed
  • HibernateSpringBootBatchInsertsEntityManager

1 file changed

+8
-7
lines changed

HibernateSpringBootBatchInsertsEntityManager/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
**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)`.
55

66
**Key points:**\
7-
- in application.properties set `spring.jpa.properties.hibernate.jdbc.batch_size`\
8-
- in application.properties set `spring.jpa.properties.hibernate.generate_statistics` (just to check that batching is working)\
9-
- in application.properties set JDBC URL with `rewriteBatchedStatements=true` (optimization for MySQL)\
10-
- 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)\
11-
- in application.properties set JDBC URL with `useServerPrepStmts=true` (this way you switch to server-side prepared statements (may lead to signnificant performance boost))\
7+
- in `application.properties` set `spring.jpa.properties.hibernate.jdbc.batch_size`\
8+
- in `application.properties` set `spring.jpa.properties.hibernate.generate_statistics` (just to check that batching is working)\
9+
- in `application.properties` set JDBC URL with `rewriteBatchedStatements=true` (optimization for MySQL)\
10+
- 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)\
11+
- in `application.properties` set JDBC URL with `useServerPrepStmts=true` (this way you switch to server-side prepared statements (may lead to signnificant performance boost))\
1212
- 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\
1313
- 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\
14-
- in DAO, flush and clear the persistence context from time to time. This way you avoid to "overwhelm" the persistence context.
14+
- in DAO, flush and clear the persistence context from time to time; this way you avoid to "overwhelm" the persistence context\
15+
- if is not needed then ensure that Second Level Cache is disabled via `spring.jpa.properties.hibernate.cache.use_second_level_cache=false`
1516

1617
**Output example:**
17-
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/HibernateSpringBootBatchInsertsEntityManager/sample.png)
18+
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/HibernateSpringBootBatchInsertsEntityManager/batch%20inserts%20via%20EntityManager.png)
1819

1920
--------------------------
2021

0 commit comments

Comments
 (0)