|
| 1 | +package org.baeldung.persistence.service; |
| 2 | + |
| 3 | +import org.baeldung.persistence.deletion.config.PersistenceJPAConfigDeletion; |
| 4 | +import org.baeldung.persistence.deletion.model.Bar; |
| 5 | +import org.baeldung.persistence.deletion.model.Baz; |
| 6 | +import org.baeldung.persistence.deletion.model.Foo; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.runner.RunWith; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.test.context.ContextConfiguration; |
| 12 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 13 | +import org.springframework.test.context.support.AnnotationConfigContextLoader; |
| 14 | +import org.springframework.transaction.PlatformTransactionManager; |
| 15 | +import org.springframework.transaction.annotation.Transactional; |
| 16 | + |
| 17 | +import javax.persistence.EntityManager; |
| 18 | +import javax.persistence.PersistenceContext; |
| 19 | + |
| 20 | +import static org.hamcrest.Matchers.notNullValue; |
| 21 | +import static org.hamcrest.Matchers.nullValue; |
| 22 | +import static org.junit.Assert.assertThat; |
| 23 | + |
| 24 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@ContextConfiguration(classes = { PersistenceJPAConfigDeletion.class }, loader = AnnotationConfigContextLoader.class) |
| 26 | +public class DeletionIntegrationTest { |
| 27 | + |
| 28 | + @PersistenceContext |
| 29 | + private EntityManager entityManager; |
| 30 | + @Autowired |
| 31 | + private PlatformTransactionManager platformTransactionManager; |
| 32 | + |
| 33 | + @Before |
| 34 | + public final void before() { |
| 35 | + entityManager.getEntityManagerFactory().getCache().evictAll(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + @Transactional |
| 40 | + public final void givenEntityIsRemoved_thenItIsNotInDB() { |
| 41 | + Foo foo = new Foo("foo"); |
| 42 | + entityManager.persist(foo); |
| 43 | + flushAndClear(); |
| 44 | + |
| 45 | + foo = entityManager.find(Foo.class, foo.getId()); |
| 46 | + assertThat(foo, notNullValue()); |
| 47 | + |
| 48 | + entityManager.remove(foo); |
| 49 | + flushAndClear(); |
| 50 | + |
| 51 | + assertThat(entityManager.find(Foo.class, foo.getId()), nullValue()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @Transactional |
| 56 | + public final void givenEntityIsRemovedAndReferencedByAnotherEntity_thenItIsNotRemoved() { |
| 57 | + Bar bar = new Bar("bar"); |
| 58 | + Foo foo = new Foo("foo"); |
| 59 | + foo.setBar(bar); |
| 60 | + entityManager.persist(foo); |
| 61 | + flushAndClear(); |
| 62 | + |
| 63 | + foo = entityManager.find(Foo.class, foo.getId()); |
| 64 | + bar = entityManager.find(Bar.class, bar.getId()); |
| 65 | + entityManager.remove(bar); |
| 66 | + flushAndClear(); |
| 67 | + |
| 68 | + bar = entityManager.find(Bar.class, bar.getId()); |
| 69 | + assertThat(bar, notNullValue()); |
| 70 | + |
| 71 | + foo = entityManager.find(Foo.class, foo.getId()); |
| 72 | + foo.setBar(null); |
| 73 | + entityManager.remove(bar); |
| 74 | + flushAndClear(); |
| 75 | + |
| 76 | + assertThat(entityManager.find(Bar.class, bar.getId()), nullValue()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + @Transactional |
| 81 | + public final void givenEntityIsRemoved_thenRemovalIsCascaded() { |
| 82 | + Bar bar = new Bar("bar"); |
| 83 | + Foo foo = new Foo("foo"); |
| 84 | + foo.setBar(bar); |
| 85 | + entityManager.persist(foo); |
| 86 | + flushAndClear(); |
| 87 | + |
| 88 | + foo = entityManager.find(Foo.class, foo.getId()); |
| 89 | + entityManager.remove(foo); |
| 90 | + flushAndClear(); |
| 91 | + |
| 92 | + assertThat(entityManager.find(Foo.class, foo.getId()), nullValue()); |
| 93 | + assertThat(entityManager.find(Bar.class, bar.getId()), nullValue()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + @Transactional |
| 98 | + public final void givenEntityIsDisassociated_thenOrphanRemovalIsApplied() { |
| 99 | + Bar bar = new Bar("bar"); |
| 100 | + Baz baz = new Baz("baz"); |
| 101 | + bar.getBazList().add(baz); |
| 102 | + entityManager.persist(bar); |
| 103 | + flushAndClear(); |
| 104 | + |
| 105 | + bar = entityManager.find(Bar.class, bar.getId()); |
| 106 | + baz = bar.getBazList().get(0); |
| 107 | + bar.getBazList().remove(baz); |
| 108 | + flushAndClear(); |
| 109 | + |
| 110 | + assertThat(entityManager.find(Baz.class, baz.getId()), nullValue()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + @Transactional |
| 115 | + public final void givenEntityIsDeletedWithJpaBulkDeleteStatement_thenItIsNotInDB() { |
| 116 | + Foo foo = new Foo("foo"); |
| 117 | + entityManager.persist(foo); |
| 118 | + flushAndClear(); |
| 119 | + |
| 120 | + entityManager.createQuery("delete from Foo where id = :id") |
| 121 | + .setParameter("id", foo.getId()) |
| 122 | + .executeUpdate(); |
| 123 | + |
| 124 | + assertThat(entityManager.find(Foo.class, foo.getId()), nullValue()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + @Transactional |
| 129 | + public final void givenEntityIsDeletedWithNativeQuery_thenItIsNotInDB() { |
| 130 | + Foo foo = new Foo("foo"); |
| 131 | + entityManager.persist(foo); |
| 132 | + flushAndClear(); |
| 133 | + |
| 134 | + entityManager.createNativeQuery("delete from FOO where ID = :id") |
| 135 | + .setParameter("id", foo.getId()) |
| 136 | + .executeUpdate(); |
| 137 | + |
| 138 | + assertThat(entityManager.find(Foo.class, foo.getId()), nullValue()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + @Transactional |
| 143 | + public final void givenEntityIsSoftDeleted_thenItIsNotReturnedFromQueries() { |
| 144 | + Foo foo = new Foo("foo"); |
| 145 | + entityManager.persist(foo); |
| 146 | + flushAndClear(); |
| 147 | + |
| 148 | + foo = entityManager.find(Foo.class, foo.getId()); |
| 149 | + foo.setDeleted(); |
| 150 | + flushAndClear(); |
| 151 | + |
| 152 | + assertThat(entityManager.find(Foo.class, foo.getId()), nullValue()); |
| 153 | + } |
| 154 | + |
| 155 | + private void flushAndClear() { |
| 156 | + entityManager.flush(); |
| 157 | + entityManager.clear(); |
| 158 | + } |
| 159 | +} |
0 commit comments