From 46f5e0dc939fe40326bc16fafa5e6b77d177e74f Mon Sep 17 00:00:00 2001 From: isaolmez Date: Fri, 26 Apr 2019 11:10:34 +0300 Subject: [PATCH] BAEL-2809: Added code samples --- .../JpaBatchInsertsIntegrationTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/batchinserts/JpaBatchInsertsIntegrationTest.java b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/batchinserts/JpaBatchInsertsIntegrationTest.java index dd61b7b6df..9e81dbc04d 100644 --- a/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/batchinserts/JpaBatchInsertsIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-2/src/test/java/com/baeldung/batchinserts/JpaBatchInsertsIntegrationTest.java @@ -9,6 +9,7 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; +import org.junit.After; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; @@ -27,16 +28,16 @@ public class JpaBatchInsertsIntegrationTest { private static final int BATCH_SIZE = 5; + @Transactional @Test public void whenInsertingSingleTypeOfEntity_thenCreatesSingleBatch() { for (int i = 0; i < 10; i++) { School school = createSchool(i); entityManager.persist(school); } - - entityManager.flush(); } + @Transactional @Test public void whenFlushingAfterBatch_ThenClearsMemory() { for (int i = 0; i < 10; i++) { @@ -48,10 +49,9 @@ public class JpaBatchInsertsIntegrationTest { School school = createSchool(i); entityManager.persist(school); } - - entityManager.flush(); } + @Transactional @Test public void whenThereAreMultipleEntities_ThenCreatesNewBatch() { for (int i = 0; i < 10; i++) { @@ -67,10 +67,9 @@ public class JpaBatchInsertsIntegrationTest { entityManager.persist(firstStudent); entityManager.persist(secondStudent); } - - entityManager.flush(); } + @Transactional @Test public void whenUpdatingEntities_thenCreatesBatch() { for (int i = 0; i < 10; i++) { @@ -86,7 +85,10 @@ public class JpaBatchInsertsIntegrationTest { for (School school : allSchools) { school.setName("Updated_" + school.getName()); } + } + @After + public void tearDown() { entityManager.flush(); } }