Skip to content

Commit 85cab60

Browse files
committed
HHH-19440 - Deprecate exposing of LockOptions
# Conflicts: # hibernate-core/src/main/java/org/hibernate/LockOptions.java
1 parent 5cad8a1 commit 85cab60

File tree

1 file changed

+95
-101
lines changed

1 file changed

+95
-101
lines changed

hibernate-core/src/main/java/org/hibernate/LockOptions.java

Lines changed: 95 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
*/
55
package org.hibernate;
66

7+
import jakarta.persistence.FindOption;
8+
import jakarta.persistence.LockOption;
79
import jakarta.persistence.PessimisticLockScope;
10+
import jakarta.persistence.RefreshOption;
811
import jakarta.persistence.Timeout;
912

1013
import java.io.Serializable;
1114
import java.util.HashMap;
1215
import java.util.Iterator;
1316
import java.util.LinkedHashMap;
1417
import java.util.Map;
15-
import java.util.Objects;
1618
import java.util.Set;
1719

1820
import static jakarta.persistence.PessimisticLockScope.NORMAL;
@@ -72,6 +74,15 @@
7274
* @see Timeouts
7375
*
7476
* @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
7586
*/
7687
@Deprecated(since = "7.0", forRemoval = true) // moving to an SPI package
7788
public class LockOptions implements Serializable {
@@ -163,7 +174,10 @@ public class LockOptions implements Serializable {
163174

164175
/**
165176
* 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
167181
*/
168182
public LockOptions() {
169183
immutable = false;
@@ -174,9 +188,11 @@ public LockOptions() {
174188

175189
/**
176190
* Construct an instance with the given {@linkplain LockMode mode}
177-
* and {@link #WAIT_FOREVER}.
191+
* and no timeout.
178192
*
179193
* @param lockMode The initial lock mode
194+
*
195+
* @see Timeouts#WAIT_FOREVER
180196
*/
181197
public LockOptions(LockMode lockMode) {
182198
immutable = false;
@@ -199,20 +215,6 @@ public LockOptions(LockMode lockMode, Timeout timeout) {
199215
pessimisticLockScope = NORMAL;
200216
}
201217

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-
216218
/**
217219
* Construct an instance with the given {@linkplain LockMode mode},
218220
* timeout, and {@linkplain PessimisticLockScope scope}.
@@ -228,6 +230,32 @@ public LockOptions(LockMode lockMode, Timeout timeout, PessimisticLockScope scop
228230
this.pessimisticLockScope = scope;
229231
}
230232

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+
231259
/**
232260
* Construct an instance with the given {@linkplain LockMode mode},
233261
* timeout, and {@linkplain PessimisticLockScope scope}.
@@ -244,27 +272,18 @@ public LockOptions(LockMode lockMode, int timeout, PessimisticLockScope scope) {
244272
}
245273

246274
/**
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
258277
*
259278
* @return {@code true} if the lock options are equivalent to
260-
* {@link LockOptions#NONE}.
279+
* {@link org.hibernate.LockOptions#NONE}.
261280
*/
262281
public boolean isEmpty() {
263282
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();
268287
}
269288

270289
/**
@@ -291,6 +310,17 @@ public LockOptions setLockMode(LockMode lockMode) {
291310
return this;
292311
}
293312

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+
294324
/**
295325
* Specify the {@link LockMode} to be used for the given query alias.
296326
*
@@ -317,43 +347,16 @@ public LockOptions setAliasSpecificLockMode(String alias, LockMode lockMode) {
317347
}
318348

319349
/**
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.
342351
*
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.
345353
*/
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();
352356
}
353357

354358
/**
355-
* Does this {@code LockOptions} instance define alias-specific lock
356-
* modes?
359+
* Whether this {@code LockOptions} instance defines alias-specific lock-modes
357360
*
358361
* @return {@code true} if this object defines alias-specific lock modes;
359362
* {@code false} otherwise.
@@ -363,12 +366,17 @@ public boolean hasAliasSpecificLockModes() {
363366
}
364367

365368
/**
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.
367374
*
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.
369377
*/
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 );
372380
}
373381

374382
/**
@@ -384,14 +392,24 @@ public Iterator<Map.Entry<String,LockMode>> getAliasLockIterator() {
384392
}
385393

386394
/**
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.
390403
*
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.
392406
*/
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;
395413
}
396414

397415
/**
@@ -453,7 +471,7 @@ public LockOptions setTimeout(Timeout timeout) {
453471
* {@link #WAIT_FOREVER}, or {@link #SKIP_LOCKED}
454472
*/
455473
public int getTimeOut() {
456-
return timeout.milliseconds();
474+
return getTimeout().milliseconds();
457475
}
458476

459477
/**
@@ -468,12 +486,10 @@ public int getTimeOut() {
468486
* @see #getTimeOut
469487
*/
470488
public LockOptions setTimeOut(int timeout) {
471-
if ( immutable ) {
472-
throw new UnsupportedOperationException("immutable global instance of LockMode");
473-
}
474489
return setTimeout( Timeouts.interpretMilliSeconds( timeout ) );
475490
}
476491

492+
477493
/**
478494
* The current lock scope:
479495
* <ul>
@@ -604,26 +620,4 @@ public static LockOptions copy(LockOptions source, LockOptions destination) {
604620
destination.setFollowOnLocking( source.getFollowOnLocking() );
605621
return destination;
606622
}
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-
}
629623
}

0 commit comments

Comments
 (0)