4
4
*/
5
5
package org .hibernate ;
6
6
7
+ import jakarta .persistence .FindOption ;
8
+ import jakarta .persistence .LockOption ;
7
9
import jakarta .persistence .PessimisticLockScope ;
10
+ import jakarta .persistence .RefreshOption ;
8
11
import jakarta .persistence .Timeout ;
9
12
10
13
import java .io .Serializable ;
11
14
import java .util .HashMap ;
12
15
import java .util .Iterator ;
13
16
import java .util .LinkedHashMap ;
14
17
import java .util .Map ;
15
- import java .util .Objects ;
16
18
import java .util .Set ;
17
19
18
20
import static jakarta .persistence .PessimisticLockScope .NORMAL ;
72
74
* @see Timeouts
73
75
*
74
76
* @author Scott Marlow
77
+ *
78
+ * @deprecated Use methods accepting {@linkplain FindOption}, {@linkplain LockOption} or {@linkplain RefreshOption}
79
+ * instead.
80
+ *
81
+ * @see Timeout
82
+ * @see Timeouts
83
+ * @see LockMode
84
+ * @see jakarta.persistence.LockModeType
85
+ * @see PessimisticLockScope
75
86
*/
76
87
@ Deprecated (since = "7.0" , forRemoval = true ) // moving to an SPI package
77
88
public class LockOptions implements Serializable {
@@ -163,7 +174,10 @@ public class LockOptions implements Serializable {
163
174
164
175
/**
165
176
* Construct an instance with mode {@link LockMode#NONE} and
166
- * timeout {@link #WAIT_FOREVER}.
177
+ * no timeout.
178
+ *
179
+ * @see LockMode#NONE
180
+ * @see Timeouts#WAIT_FOREVER
167
181
*/
168
182
public LockOptions () {
169
183
immutable = false ;
@@ -174,9 +188,11 @@ public LockOptions() {
174
188
175
189
/**
176
190
* Construct an instance with the given {@linkplain LockMode mode}
177
- * and {@link #WAIT_FOREVER} .
191
+ * and no timeout .
178
192
*
179
193
* @param lockMode The initial lock mode
194
+ *
195
+ * @see Timeouts#WAIT_FOREVER
180
196
*/
181
197
public LockOptions (LockMode lockMode ) {
182
198
immutable = false ;
@@ -199,20 +215,6 @@ public LockOptions(LockMode lockMode, Timeout timeout) {
199
215
pessimisticLockScope = NORMAL ;
200
216
}
201
217
202
- /**
203
- * Construct an instance with the given {@linkplain LockMode mode}
204
- * and timeout.
205
- *
206
- * @param lockMode The initial lock mode
207
- * @param timeout The initial timeout, in milliseconds
208
- *
209
- * @deprecated Use {@linkplain #LockOptions(LockMode, Timeout)} instead
210
- */
211
- @ Deprecated (since = "7.0" )
212
- public LockOptions (LockMode lockMode , int timeout ) {
213
- this ( lockMode , Timeouts .interpretMilliSeconds ( timeout ) );
214
- }
215
-
216
218
/**
217
219
* Construct an instance with the given {@linkplain LockMode mode},
218
220
* timeout, and {@linkplain PessimisticLockScope scope}.
@@ -228,6 +230,32 @@ public LockOptions(LockMode lockMode, Timeout timeout, PessimisticLockScope scop
228
230
this .pessimisticLockScope = scope ;
229
231
}
230
232
233
+ /**
234
+ * Internal operation used to create immutable global instances.
235
+ */
236
+ protected LockOptions (boolean immutable , LockMode lockMode ) {
237
+ this .immutable = immutable ;
238
+ this .lockMode = lockMode ;
239
+ timeout = Timeouts .WAIT_FOREVER ;
240
+ pessimisticLockScope = NORMAL ;
241
+ }
242
+
243
+
244
+ /**
245
+ * Construct an instance with the given {@linkplain LockMode mode}
246
+ * and timeout.
247
+ *
248
+ * @param lockMode The initial lock mode
249
+ * @param timeout The initial timeout, in milliseconds
250
+ *
251
+ * @deprecated Use {@linkplain #LockOptions(LockMode, Timeout)} instead
252
+ */
253
+ @ Deprecated (since = "7.0" )
254
+ public LockOptions (LockMode lockMode , int timeout ) {
255
+ this ( lockMode , Timeouts .interpretMilliSeconds ( timeout ) );
256
+ }
257
+
258
+
231
259
/**
232
260
* Construct an instance with the given {@linkplain LockMode mode},
233
261
* timeout, and {@linkplain PessimisticLockScope scope}.
@@ -244,27 +272,18 @@ public LockOptions(LockMode lockMode, int timeout, PessimisticLockScope scope) {
244
272
}
245
273
246
274
/**
247
- * Internal operation used to create immutable global instances.
248
- */
249
- private LockOptions (boolean immutable , LockMode lockMode ) {
250
- this .immutable = immutable ;
251
- this .lockMode = lockMode ;
252
- timeout = Timeouts .WAIT_FOREVER ;
253
- pessimisticLockScope = NORMAL ;
254
- }
255
-
256
- /**
257
- * Determine of the lock options are empty.
275
+ * Whether this {@code LockOptions} instance is "empty", meaning
276
+ * it has any non-default values set (which is the same as
258
277
*
259
278
* @return {@code true} if the lock options are equivalent to
260
- * {@link LockOptions#NONE}.
279
+ * {@link org.hibernate. LockOptions#NONE}.
261
280
*/
262
281
public boolean isEmpty () {
263
282
return lockMode == LockMode .NONE
264
- && timeout == Timeouts .WAIT_FOREVER
265
- && followOnLocking == null
266
- && pessimisticLockScope == NORMAL
267
- && !hasAliasSpecificLockModes ();
283
+ && timeout == Timeouts .WAIT_FOREVER
284
+ && followOnLocking == null
285
+ && pessimisticLockScope == NORMAL
286
+ && !hasAliasSpecificLockModes ();
268
287
}
269
288
270
289
/**
@@ -291,6 +310,17 @@ public LockOptions setLockMode(LockMode lockMode) {
291
310
return this ;
292
311
}
293
312
313
+ /**
314
+ * Set of {@link Map.Entry}s, each associating an alias with its
315
+ * specified {@linkplain #setAliasSpecificLockMode alias-specific}
316
+ * {@link LockMode}.
317
+ *
318
+ * @return an iterable with the {@link Map.Entry}s
319
+ */
320
+ public Set <Map .Entry <String ,LockMode >> getAliasSpecificLocks () {
321
+ return aliasSpecificLockModes == null ? emptySet () : unmodifiableSet ( aliasSpecificLockModes .entrySet () );
322
+ }
323
+
294
324
/**
295
325
* Specify the {@link LockMode} to be used for the given query alias.
296
326
*
@@ -317,43 +347,16 @@ public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) {
317
347
}
318
348
319
349
/**
320
- * Get the {@link LockMode} explicitly specified for the given alias
321
- * via {@link #setAliasSpecificLockMode(String, LockMode)}.
322
- * <p>
323
- * Differs from {@link #getEffectiveLockMode(String)} in that here we
324
- * only return an explicitly specified alias-specific lock mode.
325
- *
326
- * @param alias The alias for which to locate the explicit lock mode.
327
- * @return The explicit lock mode for that alias.
328
- */
329
- public LockMode getAliasSpecificLockMode (String alias ) {
330
- return aliasSpecificLockModes == null ? null : aliasSpecificLockModes .get ( alias );
331
- }
332
-
333
- /**
334
- * Determine the {@link LockMode} to apply to the given alias. If no
335
- * mode was {@linkplain #setAliasSpecificLockMode(String, LockMode)
336
- * explicitly set}, the {@linkplain #getLockMode() overall mode} is
337
- * returned. If the overall lock mode is also {@code null},
338
- * {@link LockMode#NONE} is returned.
339
- * <p>
340
- * Differs from {@link #getAliasSpecificLockMode(String)} in that here
341
- * we fall back to only returning the overall lock mode.
350
+ * The number of aliases that have alias-specific lock modes specified.
342
351
*
343
- * @param alias The alias for which to locate the effective lock mode.
344
- * @return The effective lock mode.
352
+ * @return the number of explicitly defined alias lock modes.
345
353
*/
346
- public LockMode getEffectiveLockMode (String alias ) {
347
- LockMode lockMode = getAliasSpecificLockMode ( alias );
348
- if ( lockMode == null ) {
349
- lockMode = this .lockMode ;
350
- }
351
- return lockMode == null ? LockMode .NONE : lockMode ;
354
+ public int getAliasLockCount () {
355
+ return aliasSpecificLockModes == null ? 0 : aliasSpecificLockModes .size ();
352
356
}
353
357
354
358
/**
355
- * Does this {@code LockOptions} instance define alias-specific lock
356
- * modes?
359
+ * Whether this {@code LockOptions} instance defines alias-specific lock-modes
357
360
*
358
361
* @return {@code true} if this object defines alias-specific lock modes;
359
362
* {@code false} otherwise.
@@ -363,12 +366,17 @@ public boolean hasAliasSpecificLockModes() {
363
366
}
364
367
365
368
/**
366
- * The number of aliases that have alias-specific lock modes specified.
369
+ * Get the {@link LockMode} explicitly specified for the given alias
370
+ * via {@link #setAliasSpecificLockMode(String, LockMode)}.
371
+ * <p>
372
+ * Differs from {@link #getEffectiveLockMode(String)} in that here we
373
+ * only return an explicitly specified alias-specific lock mode.
367
374
*
368
- * @return the number of explicitly defined alias lock modes.
375
+ * @param alias The alias for which to locate the explicit lock mode.
376
+ * @return The explicit lock mode for that alias.
369
377
*/
370
- public int getAliasLockCount ( ) {
371
- return aliasSpecificLockModes == null ? 0 : aliasSpecificLockModes .size ( );
378
+ public LockMode getAliasSpecificLockMode ( String alias ) {
379
+ return aliasSpecificLockModes == null ? null : aliasSpecificLockModes .get ( alias );
372
380
}
373
381
374
382
/**
@@ -384,14 +392,24 @@ public Iterator<Map.Entry<String,LockMode>> getAliasLockIterator() {
384
392
}
385
393
386
394
/**
387
- * Set of {@link Map.Entry}s, each associating an alias with its
388
- * specified {@linkplain #setAliasSpecificLockMode alias-specific}
389
- * {@link LockMode}.
395
+ * Determine the {@link LockMode} to apply to the given alias. If no
396
+ * mode was {@linkplain #setAliasSpecificLockMode(String, LockMode)
397
+ * explicitly set}, the {@linkplain #getLockMode() overall mode} is
398
+ * returned. If the overall lock mode is also {@code null},
399
+ * {@link LockMode#NONE} is returned.
400
+ * <p>
401
+ * Differs from {@link #getAliasSpecificLockMode(String)} in that here
402
+ * we fall back to only returning the overall lock mode.
390
403
*
391
- * @return an iterable with the {@link Map.Entry}s
404
+ * @param alias The alias for which to locate the effective lock mode.
405
+ * @return The effective lock mode.
392
406
*/
393
- public Set <Map .Entry <String ,LockMode >> getAliasSpecificLocks () {
394
- return aliasSpecificLockModes == null ? emptySet () : unmodifiableSet ( aliasSpecificLockModes .entrySet () );
407
+ public LockMode getEffectiveLockMode (String alias ) {
408
+ LockMode lockMode = getAliasSpecificLockMode ( alias );
409
+ if ( lockMode == null ) {
410
+ lockMode = this .lockMode ;
411
+ }
412
+ return lockMode == null ? LockMode .NONE : lockMode ;
395
413
}
396
414
397
415
/**
@@ -453,7 +471,7 @@ public LockOptions setTimeout(Timeout timeout) {
453
471
* {@link #WAIT_FOREVER}, or {@link #SKIP_LOCKED}
454
472
*/
455
473
public int getTimeOut () {
456
- return timeout .milliseconds ();
474
+ return getTimeout () .milliseconds ();
457
475
}
458
476
459
477
/**
@@ -468,12 +486,10 @@ public int getTimeOut() {
468
486
* @see #getTimeOut
469
487
*/
470
488
public LockOptions setTimeOut (int timeout ) {
471
- if ( immutable ) {
472
- throw new UnsupportedOperationException ("immutable global instance of LockMode" );
473
- }
474
489
return setTimeout ( Timeouts .interpretMilliSeconds ( timeout ) );
475
490
}
476
491
492
+
477
493
/**
478
494
* The current lock scope:
479
495
* <ul>
@@ -604,26 +620,4 @@ public static LockOptions copy(LockOptions source, LockOptions destination) {
604
620
destination .setFollowOnLocking ( source .getFollowOnLocking () );
605
621
return destination ;
606
622
}
607
-
608
- @ Override
609
- public boolean equals (Object object ) {
610
- if ( this == object ) {
611
- return true ;
612
- }
613
- else if ( !(object instanceof LockOptions that ) ) {
614
- return false ;
615
- }
616
- else {
617
- return timeout == that .timeout
618
- && pessimisticLockScope == that .pessimisticLockScope
619
- && lockMode == that .lockMode
620
- && Objects .equals ( aliasSpecificLockModes , that .aliasSpecificLockModes )
621
- && Objects .equals ( followOnLocking , that .followOnLocking );
622
- }
623
- }
624
-
625
- @ Override
626
- public int hashCode () {
627
- return Objects .hash ( lockMode , timeout , aliasSpecificLockModes , followOnLocking , pessimisticLockScope );
628
- }
629
623
}
0 commit comments