diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/ExtendedPersistenceContextUserService.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/ExtendedPersistenceContextUserService.java index 8ac2769a3f..ef25aac69f 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/ExtendedPersistenceContextUserService.java +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/ExtendedPersistenceContextUserService.java @@ -15,10 +15,6 @@ public class ExtendedPersistenceContextUserService { @PersistenceContext(type = PersistenceContextType.EXTENDED) private EntityManager entityManager; - public EntityManager getEntityManager() { - return entityManager; - } - @Transactional public User insertWithTransaction(User user) { entityManager.persist(user); diff --git a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/TransctionPersistenceContextUserService.java b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/TransctionPersistenceContextUserService.java index cd122d7fcb..481defcf08 100644 --- a/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/TransctionPersistenceContextUserService.java +++ b/persistence-modules/spring-boot-persistence/src/main/java/com/baeldung/persistencecontext/service/TransctionPersistenceContextUserService.java @@ -15,10 +15,6 @@ public class TransctionPersistenceContextUserService { @PersistenceContext private EntityManager entityManager; - public EntityManager getEntityManager() { - return entityManager; - } - @Transactional public User insertWithTransaction(User user) { entityManager.persist(user); diff --git a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java index fa35807d47..98a1a4a6be 100644 --- a/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java +++ b/persistence-modules/spring-boot-persistence/src/test/java/com/baeldung/persistencecontext/PersistenceContextIntegrationTest.java @@ -40,7 +40,7 @@ public class PersistenceContextIntegrationTest { } @Test(expected = HttpServerErrorException.class) - public void testThatWhenUserSaveWithOutTransactionInPersistenceContextTypeTransactionThenShouldFailOnPersist() { + public void testThatWhenUserSaveWithOutTransactionInPersistenceContextTypeTransactionThenShouldWhtowTransactionRequiredExceptionOnPersist() { User user = new User(122L, "Devender", "admin"); restTemplate.postForEntity(buildRequestUrl("v4/user/"), user, User.class); } @@ -55,6 +55,21 @@ public class PersistenceContextIntegrationTest { assertThat(userFromTransctionPersistenceContext, Is.is(IsNull.nullValue())); } + @Test(expected = HttpServerErrorException.class) + public void testThatWhenAddUserWithSameIdentifierInPersistenceContextThenShouldThrowException() { + User user = new User(126L, "Devender", "admin"); + restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class); + restTemplate.postForEntity(buildRequestUrl("v2/user/"), user, User.class); + } + + @Test + public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldSaveEntityIntoDB() { + User user = new User(127L, "Devender", "admin"); + restTemplate.postForEntity(buildRequestUrl("v3/user/"), user, User.class); + User userFromDB = restTemplate.getForObject(buildRequestUrl("v1/user/127"), User.class); + assertThat(userFromDB, Is.is(IsNull.notNullValue())); + } + @Test public void testThatWhenUserSavedWithExtendedPersistenceContextWithTransactionThenUserShouldFlushCachedEntityIntoDB() { User user = new User(124L, "Devender", "admin");