|
| 1 | +package com.baeldung.hibernate.lifecycle; |
| 2 | + |
| 3 | +import org.hibernate.Session; |
| 4 | +import org.hibernate.SessionFactory; |
| 5 | +import org.hibernate.Transaction; |
| 6 | +import org.hibernate.engine.spi.Status; |
| 7 | +import org.junit.AfterClass; |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import static com.baeldung.hibernate.lifecycle.DirtyDataInspector.getDirtyEntities; |
| 15 | +import static com.baeldung.hibernate.lifecycle.HibernateLifecycleUtil.*; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +public class HibernateLifecycleUnitTest { |
| 19 | + |
| 20 | + @BeforeClass |
| 21 | + public static void setup() throws Exception { |
| 22 | + HibernateLifecycleUtil.init(); |
| 23 | + |
| 24 | + } |
| 25 | + |
| 26 | + @AfterClass |
| 27 | + public static void tearDown() throws Exception { |
| 28 | + HibernateLifecycleUtil.tearDown(); |
| 29 | + } |
| 30 | + |
| 31 | + @Before |
| 32 | + public void beforeMethod() { |
| 33 | + DirtyDataInspector.clearDirtyEntitites(); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void whenEntityLoaded_thenEntityManaged() throws Exception { |
| 38 | + SessionFactory sessionFactory = HibernateLifecycleUtil.getSessionFactory(); |
| 39 | + try (Session session = sessionFactory.openSession()) { |
| 40 | + Transaction transaction = startTransaction(session); |
| 41 | + |
| 42 | + assertThat(getManagedEntities(session)).isEmpty(); |
| 43 | + |
| 44 | + List<FootballPlayer> players = session.createQuery("from FootballPlayer").getResultList(); |
| 45 | + assertThat(getManagedEntities(session)).size().isEqualTo(3); |
| 46 | + |
| 47 | + assertThat(getDirtyEntities()).isEmpty(); |
| 48 | + |
| 49 | + FootballPlayer gigiBuffon = players.stream().filter(p -> p.getId() == 3).findFirst().get(); |
| 50 | + |
| 51 | + gigiBuffon.setName("Gianluigi Buffon"); |
| 52 | + transaction.commit(); |
| 53 | + |
| 54 | + assertThat(getDirtyEntities()).size().isEqualTo(1); |
| 55 | + assertThat(getDirtyEntities().get(0).getId()).isEqualTo(3); |
| 56 | + assertThat(getDirtyEntities().get(0).getName()).isEqualTo("Gianluigi Buffon"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void whenDetached_thenNotTracked() throws Exception { |
| 62 | + SessionFactory sessionFactory = HibernateLifecycleUtil.getSessionFactory(); |
| 63 | + try (Session session = sessionFactory.openSession()) { |
| 64 | + Transaction transaction = startTransaction(session); |
| 65 | + |
| 66 | + FootballPlayer cr7 = session.get(FootballPlayer.class, 1L); |
| 67 | + assertThat(getManagedEntities(session)).size().isEqualTo(1); |
| 68 | + assertThat(getManagedEntities(session).get(0).getId()).isEqualTo(cr7.getId()); |
| 69 | + |
| 70 | + session.evict(cr7); |
| 71 | + assertThat(getManagedEntities(session)).size().isEqualTo(0); |
| 72 | + |
| 73 | + cr7.setName("CR7"); |
| 74 | + transaction.commit(); |
| 75 | + |
| 76 | + assertThat(getDirtyEntities()).isEmpty(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void whenReattached_thenTrackedAgain() throws Exception { |
| 82 | + SessionFactory sessionFactory = HibernateLifecycleUtil.getSessionFactory(); |
| 83 | + try (Session session = sessionFactory.openSession()) { |
| 84 | + Transaction transaction = startTransaction(session); |
| 85 | + |
| 86 | + FootballPlayer messi = session.get(FootballPlayer.class, 2L); |
| 87 | + |
| 88 | + session.evict(messi); |
| 89 | + messi.setName("Leo Messi"); |
| 90 | + transaction.commit(); |
| 91 | + assertThat(getDirtyEntities()).isEmpty(); |
| 92 | + |
| 93 | + transaction = startTransaction(session); |
| 94 | + session.update(messi); |
| 95 | + transaction.commit(); |
| 96 | + assertThat(getDirtyEntities()).size().isEqualTo(1); |
| 97 | + assertThat(getDirtyEntities().get(0).getName()).isEqualTo("Leo Messi"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void givenNewEntityWithID_whenReattached_thenManaged() throws Exception { |
| 103 | + SessionFactory sessionFactory = HibernateLifecycleUtil.getSessionFactory(); |
| 104 | + try (Session session = sessionFactory.openSession()) { |
| 105 | + Transaction transaction = startTransaction(session); |
| 106 | + |
| 107 | + FootballPlayer gigi = new FootballPlayer(); |
| 108 | + gigi.setId(3); |
| 109 | + gigi.setName("Gigi the Legend"); |
| 110 | + |
| 111 | + session.update(gigi); |
| 112 | + assertThat(getManagedEntities(session)).size().isEqualTo(1); |
| 113 | + |
| 114 | + transaction.commit(); |
| 115 | + assertThat(getDirtyEntities()).size().isEqualTo(1); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void givenTransientEntity_whenSave_thenManaged() throws Exception { |
| 121 | + SessionFactory sessionFactory = HibernateLifecycleUtil.getSessionFactory(); |
| 122 | + try (Session session = sessionFactory.openSession()) { |
| 123 | + Transaction transaction = startTransaction(session); |
| 124 | + |
| 125 | + FootballPlayer neymar = new FootballPlayer(); |
| 126 | + neymar.setName("Neymar"); |
| 127 | + |
| 128 | + session.save(neymar); |
| 129 | + assertThat(getManagedEntities(session)).size().isEqualTo(1); |
| 130 | + assertThat(neymar.getId()).isNotNull(); |
| 131 | + |
| 132 | + int count = queryCount("select count(*) from Football_Player where name='Neymar'"); |
| 133 | + assertThat(count).isEqualTo(0); |
| 134 | + |
| 135 | + transaction.commit(); |
| 136 | + |
| 137 | + count = queryCount("select count(*) from Football_Player where name='Neymar'"); |
| 138 | + assertThat(count).isEqualTo(1); |
| 139 | + |
| 140 | + transaction = startTransaction(session); |
| 141 | + session.delete(neymar); |
| 142 | + transaction.commit(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + @Test() |
| 147 | + public void whenDelete_thenMarkDeleted() throws Exception { |
| 148 | + SessionFactory sessionFactory = HibernateLifecycleUtil.getSessionFactory(); |
| 149 | + try (Session session = sessionFactory.openSession()) { |
| 150 | + Transaction transaction = startTransaction(session); |
| 151 | + |
| 152 | + FootballPlayer neymar = new FootballPlayer(); |
| 153 | + neymar.setName("Neymar"); |
| 154 | + |
| 155 | + session.save(neymar); |
| 156 | + transaction.commit(); |
| 157 | + |
| 158 | + transaction = startTransaction(session); |
| 159 | + session.delete(neymar); |
| 160 | + assertThat(getManagedEntities(session).get(0).getStatus()).isEqualTo(Status.DELETED); |
| 161 | + transaction.commit(); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments