88import org .junit .Test ;
99import org .junit .runner .RunWith ;
1010import org .springframework .beans .factory .annotation .Autowired ;
11+ import org .springframework .test .annotation .Commit ;
1112import org .springframework .test .context .ContextConfiguration ;
1213import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
1314import org .springframework .test .context .support .AnnotationConfigContextLoader ;
15+ import org .springframework .test .context .transaction .TestTransaction ;
1416import org .springframework .transaction .annotation .Transactional ;
1517
18+ import static junit .framework .TestCase .assertFalse ;
19+ import static junit .framework .TestCase .assertTrue ;
20+
1621@ RunWith (SpringJUnit4ClassRunner .class )
1722@ ContextConfiguration (classes = { HibernateConf .class })
1823@ Transactional
@@ -35,4 +40,146 @@ public void whenBootstrapHibernateSession_thenNoException() {
3540 Assert .assertNotNull (searchEntity );
3641 }
3742
43+ @ Test
44+ public void whenProgrammaticTransactionCommit_thenEntityIsInDatabase () {
45+ assertTrue (TestTransaction .isActive ());
46+
47+ //Save an entity and commit.
48+ Session session = sessionFactory .getCurrentSession ();
49+
50+ TestEntity newEntity = new TestEntity ();
51+ newEntity .setId (1 );
52+ session .save (newEntity );
53+
54+ TestEntity searchEntity = session .find (TestEntity .class , 1 );
55+
56+ Assert .assertNotNull (searchEntity );
57+ assertTrue (TestTransaction .isFlaggedForRollback ());
58+
59+ TestTransaction .flagForCommit ();
60+ TestTransaction .end ();
61+
62+ assertFalse (TestTransaction .isFlaggedForRollback ());
63+ assertFalse (TestTransaction .isActive ());
64+
65+ //Check that the entity is still there in a new transaction,
66+ //then delete it, but don't commit.
67+ TestTransaction .start ();
68+
69+ assertTrue (TestTransaction .isFlaggedForRollback ());
70+ assertTrue (TestTransaction .isActive ());
71+
72+ session = sessionFactory .getCurrentSession ();
73+ searchEntity = session .find (TestEntity .class , 1 );
74+
75+ Assert .assertNotNull (searchEntity );
76+
77+ session .delete (searchEntity );
78+ session .flush ();
79+
80+ TestTransaction .end ();
81+
82+ assertFalse (TestTransaction .isActive ());
83+
84+ //Check that the entity is still there in a new transaction,
85+ //then delete it and commit.
86+ TestTransaction .start ();
87+
88+ session = sessionFactory .getCurrentSession ();
89+ searchEntity = session .find (TestEntity .class , 1 );
90+
91+ Assert .assertNotNull (searchEntity );
92+
93+ session .delete (searchEntity );
94+ session .flush ();
95+
96+ assertTrue (TestTransaction .isActive ());
97+
98+ TestTransaction .flagForCommit ();
99+ TestTransaction .end ();
100+
101+ assertFalse (TestTransaction .isActive ());
102+
103+ //Check that the entity is no longer there in a new transaction.
104+ TestTransaction .start ();
105+
106+ assertTrue (TestTransaction .isActive ());
107+
108+ session = sessionFactory .getCurrentSession ();
109+ searchEntity = session .find (TestEntity .class , 1 );
110+
111+ Assert .assertNull (searchEntity );
112+ }
113+
114+ @ Test
115+ @ Commit
116+ public void givenTransactionCommitDefault_whenProgrammaticTransactionCommit_thenEntityIsInDatabase () {
117+ assertTrue (TestTransaction .isActive ());
118+
119+ //Save an entity and commit.
120+ Session session = sessionFactory .getCurrentSession ();
121+
122+ TestEntity newEntity = new TestEntity ();
123+ newEntity .setId (1 );
124+ session .save (newEntity );
125+
126+ TestEntity searchEntity = session .find (TestEntity .class , 1 );
127+
128+ Assert .assertNotNull (searchEntity );
129+ assertFalse (TestTransaction .isFlaggedForRollback ());
130+
131+ TestTransaction .end ();
132+
133+ assertFalse (TestTransaction .isFlaggedForRollback ());
134+ assertFalse (TestTransaction .isActive ());
135+
136+ //Check that the entity is still there in a new transaction,
137+ //then delete it, but don't commit.
138+ TestTransaction .start ();
139+
140+ assertFalse (TestTransaction .isFlaggedForRollback ());
141+ assertTrue (TestTransaction .isActive ());
142+
143+ session = sessionFactory .getCurrentSession ();
144+ searchEntity = session .find (TestEntity .class , 1 );
145+
146+ Assert .assertNotNull (searchEntity );
147+
148+ session .delete (searchEntity );
149+ session .flush ();
150+
151+ TestTransaction .flagForRollback ();
152+ TestTransaction .end ();
153+
154+ assertFalse (TestTransaction .isActive ());
155+
156+ //Check that the entity is still there in a new transaction,
157+ //then delete it and commit.
158+ TestTransaction .start ();
159+
160+ session = sessionFactory .getCurrentSession ();
161+ searchEntity = session .find (TestEntity .class , 1 );
162+
163+ Assert .assertNotNull (searchEntity );
164+
165+ session .delete (searchEntity );
166+ session .flush ();
167+
168+ assertTrue (TestTransaction .isActive ());
169+
170+ TestTransaction .end ();
171+
172+ assertFalse (TestTransaction .isActive ());
173+
174+ //Check that the entity is no longer there in a new transaction.
175+ TestTransaction .start ();
176+
177+ assertTrue (TestTransaction .isActive ());
178+
179+ session = sessionFactory .getCurrentSession ();
180+ searchEntity = session .find (TestEntity .class , 1 );
181+
182+ Assert .assertNull (searchEntity );
183+ }
184+
38185}
0 commit comments