44 */
55package org .hibernate .orm .test .loading .multiLoad ;
66
7- import static org .junit .Assert .assertEquals ;
8- import static org .junit .Assert .assertNotNull ;
9- import static org .junit .Assert .assertTrue ;
7+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8+ import static org .junit .jupiter . api . Assertions .assertNotNull ;
9+ import static org .junit .jupiter . api . Assertions .assertTrue ;
1010
1111import java .util .List ;
1212import java .util .stream .Collectors ;
2323import org .hibernate .testing .orm .junit .SessionFactory ;
2424import org .hibernate .testing .orm .junit .SessionFactoryScope ;
2525import org .hibernate .testing .orm .junit .Setting ;
26+ import org .junit .jupiter .api .AfterEach ;
2627import org .junit .jupiter .api .BeforeEach ;
2728import org .junit .jupiter .api .Test ;
2829
4950@ JiraKey (value = "HHH-18992" )
5051public class MultiLoadLockingTest {
5152
52- private List <Customer > customerList = List .of (
53+ private final List <Customer > customerList = List .of (
5354 new Customer (1L , "Customer A" ),
5455 new Customer (2L , "Customer B" ),
5556 new Customer (3L , "Customer C" ),
5657 new Customer (4L , "Customer D" ),
5758 new Customer (5L , "Customer E" )
5859 );
5960
60- private List <Long > customerIds = customerList
61+ private final List <Long > customerIds = customerList
6162 .stream ()
6263 .map (Customer ::getId )
6364 .collect (Collectors .toList ());
6465
65- private List <EntityWithAggregateId > entityWithAggregateIdList = List .of (
66+ private final List <EntityWithAggregateId > entityWithAggregateIdList = List .of (
6667 new EntityWithAggregateId ( new EntityWithAggregateId .Key ( "1" , "1" ), "Entity A" ),
6768 new EntityWithAggregateId ( new EntityWithAggregateId .Key ( "2" , "2" ), "Entity B" ),
6869 new EntityWithAggregateId ( new EntityWithAggregateId .Key ( "3" , "3" ), "Entity C" ),
6970 new EntityWithAggregateId ( new EntityWithAggregateId .Key ( "4" , "4" ), "Entity D" ),
7071 new EntityWithAggregateId ( new EntityWithAggregateId .Key ( "5" , "5" ), "Entity E" )
7172 );
7273
73- private List <EntityWithAggregateId .Key > entityWithAggregateIdKeys = entityWithAggregateIdList
74+ private final List <EntityWithAggregateId .Key > entityWithAggregateIdKeys = entityWithAggregateIdList
7475 .stream ()
7576 .map (EntityWithAggregateId ::getKey )
7677 .collect (Collectors .toList ());
7778
78- public List <User > userList = List .of (
79+ public final List <User > userList = List .of (
7980 new User (1 , null ),
8081 new User (2 , null ),
8182 new User (3 , null ),
8283 new User (4 , null ),
8384 new User (5 , null )
8485 );
8586
86- private List <Integer > userIds = userList
87+ private final List <Integer > userIds = userList
8788 .stream ()
8889 .map (User ::getId )
8990 .collect (Collectors .toList ());
@@ -99,8 +100,17 @@ public void prepareTestDataAndClearL2C(SessionFactoryScope scope) {
99100 scope .getSessionFactory ().getCache ().evictAll ();
100101 }
101102
102- // (1) simple Id entity w/ pessimistic read lock
103+ @ AfterEach
104+ public void tearDown (SessionFactoryScope scope ) {
105+ scope .inTransaction (session -> {
106+ session .createMutationQuery ( "delete from Customer" ).executeUpdate ();
107+ session .createMutationQuery ( "delete from EntityWithAggregateId" ).executeUpdate ();
108+ session .createMutationQuery ( "delete from User" ).executeUpdate ();
109+ });
110+ }
111+
103112
113+ // (1) simple Id entity w/ pessimistic read lock
104114 @ Test
105115 void testMultiLoadSimpleIdEntityPessimisticReadLock (SessionFactoryScope scope ) {
106116 scope .inTransaction ( session -> {
@@ -109,14 +119,11 @@ void testMultiLoadSimpleIdEntityPessimisticReadLock(SessionFactoryScope scope) {
109119 .multiLoad (customerIds );
110120 assertNotNull (customersLoaded );
111121 assertEquals (customerList .size (), customersLoaded .size ());
112- customersLoaded .forEach (customer -> {
113- assertEquals (LockMode .PESSIMISTIC_READ , session .getCurrentLockMode (customer ));
114- });
122+ customersLoaded .forEach (customer -> assertEquals (LockMode .PESSIMISTIC_READ , session .getCurrentLockMode (customer )) );
115123 } );
116124 }
117125
118126 // (2) composite Id entity w/ pessimistic read lock (one of the entities already in L1C)
119-
120127 @ Test
121128 void testMultiLoadCompositeIdEntityPessimisticReadLockAlreadyInSession (
122129 SessionFactoryScope scope ) {
@@ -130,14 +137,11 @@ void testMultiLoadCompositeIdEntityPessimisticReadLockAlreadyInSession(
130137 .multiLoad (entityWithAggregateIdKeys );
131138 assertNotNull (entitiesLoaded );
132139 assertEquals (entityWithAggregateIdList .size (), entitiesLoaded .size ());
133- entitiesLoaded .forEach (entity -> {
134- assertEquals (LockMode .PESSIMISTIC_READ , session .getCurrentLockMode (entity ));
135- });
140+ entitiesLoaded .forEach (entity -> assertEquals (LockMode .PESSIMISTIC_READ , session .getCurrentLockMode (entity )) );
136141 } );
137142 }
138143
139144 // (3) simple Id entity w/ pessimistic write lock (one in L1C & some in L2C)
140-
141145 @ Test
142146 public void testMultiLoadSimpleIdEntityPessimisticWriteLockSomeInL1CAndSomeInL2C (
143147 SessionFactoryScope scope ) {
@@ -152,56 +156,18 @@ public void testMultiLoadSimpleIdEntityPessimisticWriteLockSomeInL1CAndSomeInL2C
152156 User userInL1C = session .find (User .class , userInL1CId );
153157 assertNotNull (userInL1C );
154158 List <User > usersLoaded = session .byMultipleIds (User .class )
159+ .enableSessionCheck ( true )
155160 .with (new LockOptions (LockMode .PESSIMISTIC_WRITE ))
156161 .multiLoad (userIds );
157162 assertNotNull (usersLoaded );
158163 assertEquals (userList .size (), usersLoaded .size ());
159- usersLoaded .forEach (user -> {
160- assertEquals (LockMode .PESSIMISTIC_WRITE , session .getCurrentLockMode (user ));
161- });
164+ usersLoaded .forEach (user -> assertEquals (LockMode .PESSIMISTIC_WRITE , session .getCurrentLockMode (user )) );
162165 } );
163166 }
164167
165168
166-
167- // (4) simple Id entity w/ optimistic read lock
168-
169- @ Test
170- void testMultiLoadSimpleIdEntityOptimisticReadLock (SessionFactoryScope scope ) {
171- scope .inTransaction ( session -> {
172- List <Customer > customersLoaded = session .byMultipleIds (Customer .class )
173- .with (new LockOptions (LockMode .OPTIMISTIC ))
174- .multiLoad (customerIds );
175- assertNotNull (customersLoaded );
176- assertEquals (customerList .size (), customersLoaded .size ());
177- customersLoaded .forEach (customer -> {
178- assertEquals (LockMode .OPTIMISTIC , session .getCurrentLockMode (customer ));
179- });
180- } );
181- }
182-
183-
184- // (5) simple Id entity w/ optimistic force increment lock
185-
186- @ Test
187- void testMultiLoadSimpleIdEntityOptimisticForceIncrementLock (SessionFactoryScope scope ) {
188- scope .inTransaction ( session -> {
189- List <Customer > customersLoaded = session .byMultipleIds (Customer .class )
190- .with (new LockOptions (LockMode .OPTIMISTIC_FORCE_INCREMENT ))
191- .multiLoad (customerIds );
192- assertNotNull (customersLoaded );
193- assertEquals (customerList .size (), customersLoaded .size ());
194- customersLoaded .forEach (customer -> {
195- assertEquals (LockMode .OPTIMISTIC_FORCE_INCREMENT , session .getCurrentLockMode (customer ));
196- });
197- } );
198- }
199-
200-
201-
202- @ Entity
169+ @ Entity ( name = "Customer" )
203170 public static class Customer {
204-
205171 @ Id
206172 private Long id ;
207173 @ Basic
@@ -230,6 +196,6 @@ public String getName() {
230196 public void setName (String name ) {
231197 this .name = name ;
232198 }
233-
234199 }
200+
235201}
0 commit comments