Skip to content

Commit f88b739

Browse files
committed
HHH-19440 - Deprecate exposing of LockOptions
1 parent 7d43ce8 commit f88b739

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
*/
55
package org.hibernate;
66

7-
import jakarta.persistence.FindOption;
87
import jakarta.persistence.PessimisticLockScope;
9-
import jakarta.persistence.RefreshOption;
108
import jakarta.persistence.Timeout;
119
import org.hibernate.query.Query;
1210

@@ -52,7 +50,7 @@
5250
*
5351
* @author Scott Marlow
5452
*/
55-
public class LockOptions implements FindOption, RefreshOption, Serializable {
53+
public class LockOptions implements Serializable {
5654
/**
5755
* Represents {@link LockMode#NONE}, to which timeout and scope are
5856
* not applicable.

hibernate-core/src/test/java/org/hibernate/orm/test/jpa/lock/LockExceptionTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@
44
*/
55
package org.hibernate.orm.test.jpa.lock;
66

7+
import jakarta.persistence.LockModeType;
8+
import jakarta.persistence.LockTimeoutException;
9+
import jakarta.persistence.PessimisticLockException;
710
import org.hibernate.Timeouts;
811
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
912
import org.hibernate.cfg.AvailableSettings;
1013
import org.hibernate.dialect.CockroachDialect;
1114
import org.hibernate.dialect.SQLServerDialect;
1215
import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
16+
import org.hibernate.exception.LockAcquisitionException;
1317
import org.hibernate.orm.test.jpa.model.AbstractJPATest;
1418
import org.hibernate.orm.test.jpa.model.Item;
15-
16-
import org.hibernate.testing.orm.junit.JiraKey;
1719
import org.hibernate.testing.jdbc.SQLServerSnapshotIsolationConnectionProvider;
1820
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
21+
import org.hibernate.testing.orm.junit.JiraKey;
1922
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
2023
import org.hibernate.testing.orm.junit.SkipForDialect;
2124
import org.hibernate.testing.transaction.TransactionUtil2;
2225
import org.junit.jupiter.api.AfterAll;
2326
import org.junit.jupiter.api.Test;
2427

25-
import jakarta.persistence.LockModeType;
26-
import jakarta.persistence.LockTimeoutException;
27-
import jakarta.persistence.PessimisticLockException;
28-
2928
import static org.junit.jupiter.api.Assertions.fail;
3029

3130
/**
@@ -78,7 +77,7 @@ public void testLockTimeoutFind() {
7877
);
7978
fail( "Expecting a failure" );
8079
}
81-
catch (LockTimeoutException | PessimisticLockException | org.hibernate.exception.LockTimeoutException expected ) {
80+
catch (LockTimeoutException | PessimisticLockException | LockAcquisitionException expected ) {
8281
// expected outcome
8382
}
8483
}

migration-guide.adoc

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,11 +729,47 @@ Now, `varchar(1)` is used by default.
729729

730730

731731
[[settings]]
732-
=== Changes Related to Settings
732+
== Changes Related to Settings
733733

734734
* Removed `hibernate.mapping.precedence` and friends
735735
* Removed `hibernate.allow_refresh_detached_entity`
736736

737+
[[lock-options]]
738+
== LockOptions
739+
740+
The exposure of `LockOptions` as an API has been deprecated. Applications should instead prefer the use of `LockMode` (or `LockModeType`), `Timeout` and `PessimisticLockScope`.
741+
742+
Use this
743+
744+
====
745+
[source, java, indent=0]
746+
----
747+
Book loaded = session.find(
748+
Book.class,
749+
1,
750+
LockMode.PESSIMISTIC_WRITE,
751+
Timeouts.NO_WAIT
752+
);
753+
----
754+
====
755+
756+
instead of this
757+
758+
====
759+
[source, java, indent=0]
760+
----
761+
Book loaded = session.find(
762+
Book.class,
763+
1,
764+
new LockOptions(
765+
LockMode.PESSIMISTIC_WRITE,
766+
0
767+
)
768+
);
769+
----
770+
====
771+
772+
See the link:{whatsNewBase}#operation-options[What's New] guide for more details.
737773

738774

739775
[[pools]]

whats-new.adoc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,53 @@ The new operations `Session.findMultiple()` and `StatelessSession.getMultiple()`
126126
Combined with the `BatchSize` option, allows breaking up the JDBC calls into "batches".
127127

128128

129+
[[operation-options]]
130+
== FindOption, LockOption, RefreshOption
131+
132+
As a more typesafe replacement for hints, Jakarta Persistence now allows for "option objects" which indicate these hints. Not only is it more typesafe, but also more concise - a double win!
133+
134+
These "option objects" come in 3 flavors depending on what operation is being performed -
135+
136+
FindOption:: Used to indicate options for find operations :
137+
* `Session.find`
138+
* `Session.findMultiple`
139+
* etc
140+
LockOption:: Used to indicate options for `Session.lock`
141+
RefreshOption:: Used to indicate options for `Session.refresh`
142+
143+
Each of these are interfaces. Jakarta Persistence does provide some built-in options:
144+
145+
CacheRetrieveMode:: Is now a `FindOption`.
146+
CacheStoreMode:: Is now a `FindOption` and a `RefreshOption`.
147+
LockModeType:: Is now a `FindOption` and a `RefreshOption`.
148+
Timeout (new):: Is a `FindOption`, a `RefreshOption` and a `LockOption`. See also `org.hibernate.Timeouts` for some magic values.
149+
PessimisticLockScope:: Is now a `FindOption`, a `RefreshOption` and a `LockOption`
150+
151+
Hibernate provides a few additional options:
152+
153+
ReadOnlyMode (new):: Is a `FindOption` which indicates whether the entities and collections being loaded should be considered read-only.
154+
EnabledFetchProfile (new):: Is a `FindOption` indicating a fetch-profile to be enabled for the find operation.
155+
LockMode:: Is now a `FindOption` and a `RefreshOption` (expanded version of JPA's `LockModeType`).
156+
CacheMode:: Is now a `FindOption`.
157+
BatchSize (new):: Is now a `FindOption`.
158+
159+
160+
All operations which accept options (which previously accepted hints) accept a varargs grouping of them. E.g.
161+
162+
====
163+
[source, java, indent=0]
164+
----
165+
Book loaded = session.find(
166+
Book.class,
167+
1,
168+
LockMode.PESSIMISTIC_WRITE,
169+
Timeouts.NO_WAIT,
170+
new EnabledFetchProfile("with-authors")
171+
);
172+
----
173+
====
174+
175+
129176
[[QuerySpecification]]
130177
== QuerySpecification, Restriction, and Range
131178

0 commit comments

Comments
 (0)