10
10
import java .util .List ;
11
11
import java .util .function .BiFunction ;
12
12
import java .util .function .Function ;
13
+ import javax .persistence .CacheRetrieveMode ;
14
+ import javax .persistence .CacheStoreMode ;
13
15
import javax .persistence .EntityGraph ;
16
+ import javax .persistence .FlushModeType ;
17
+ import javax .persistence .LockModeType ;
14
18
import javax .persistence .Parameter ;
15
19
import javax .persistence .criteria .CriteriaBuilder ;
16
20
import javax .persistence .criteria .CriteriaDelete ;
31
35
import org .hibernate .engine .spi .PersistentAttributeInterceptable ;
32
36
import org .hibernate .engine .spi .PersistentAttributeInterceptor ;
33
37
import org .hibernate .engine .spi .SharedSessionContractImplementor ;
38
+ import org .hibernate .jpa .internal .util .FlushModeTypeHelper ;
34
39
import org .hibernate .proxy .HibernateProxy ;
35
40
import org .hibernate .reactive .common .AffectedEntities ;
36
41
import org .hibernate .reactive .common .Identifier ;
42
47
import io .smallrye .mutiny .Uni ;
43
48
import org .hibernate .stat .Statistics ;
44
49
50
+ import static org .hibernate .internal .util .LockModeConverter .convertToLockMode ;
51
+ import static org .hibernate .jpa .internal .util .CacheModeHelper .interpretCacheMode ;
52
+ import static org .hibernate .jpa .internal .util .CacheModeHelper .interpretCacheRetrieveMode ;
53
+ import static org .hibernate .jpa .internal .util .CacheModeHelper .interpretCacheStoreMode ;
54
+
45
55
/**
46
56
* An API for Hibernate Reactive where non-blocking operations are
47
57
* represented by a Mutiny {@link Uni}.
@@ -243,6 +253,22 @@ interface Query<R> {
243
253
*/
244
254
Query <R > setCacheMode (CacheMode cacheMode );
245
255
256
+ /**
257
+ * Set the current {@link CacheStoreMode} in effect while this query
258
+ * is being executed.
259
+ */
260
+ default Query <R > setCacheStoreMode (CacheStoreMode cacheStoreMode ) {
261
+ return setCacheMode ( interpretCacheMode ( cacheStoreMode , interpretCacheRetrieveMode (getCacheMode ()) ) );
262
+ }
263
+
264
+ /**
265
+ * Set the current {@link CacheRetrieveMode} in effect while this query
266
+ * is being executed.
267
+ */
268
+ default Query <R > setCacheRetrieveMode (CacheRetrieveMode cacheRetrieveMode ) {
269
+ return setCacheMode ( interpretCacheMode ( interpretCacheStoreMode (getCacheMode ()), cacheRetrieveMode ) );
270
+ }
271
+
246
272
/**
247
273
* Obtain the {@link CacheMode} in effect for this query. By default,
248
274
* the query inherits the {@code CacheMode} of the {@link Session}
@@ -258,6 +284,14 @@ interface Query<R> {
258
284
*/
259
285
Query <R > setFlushMode (FlushMode flushMode );
260
286
287
+
288
+ /**
289
+ * Set the current {@link FlushModeType} in effect while this query is
290
+ * being executed.
291
+ */
292
+ default Query <R > setFlushMode (FlushModeType flushModeType ) {
293
+ return setFlushMode ( FlushModeTypeHelper .getFlushMode (flushModeType ) );
294
+ }
261
295
/**
262
296
* Obtain the {@link FlushMode} in effect for this query. By default,
263
297
* the query inherits the {@code FlushMode} of the {@link Session}
@@ -272,6 +306,13 @@ interface Query<R> {
272
306
*/
273
307
Query <R > setLockMode (LockMode lockMode );
274
308
309
+ /**
310
+ * Set the {@link LockModeType} to use for the whole query.
311
+ */
312
+ default Query <R > setLockMode (LockModeType lockModeType ) {
313
+ return setLockMode ( convertToLockMode (lockModeType ) );
314
+ }
315
+
275
316
/**
276
317
* Set the {@link LockMode} to use for specified alias (as defined in
277
318
* the query's {@code from} clause).
@@ -283,6 +324,19 @@ interface Query<R> {
283
324
*/
284
325
Query <R > setLockMode (String alias , LockMode lockMode );
285
326
327
+ /**
328
+ * Set the {@link LockModeType} to use for specified alias (as defined in
329
+ * the query's {@code from} clause).
330
+ *
331
+ * @param alias the from clause alias
332
+ * @param lockModeType the requested {@link LockModeType}
333
+ *
334
+ * @see org.hibernate.query.Query#setLockMode(String,LockMode)
335
+ */
336
+ default Query <R > setLockMode (String alias , LockModeType lockModeType ) {
337
+ return setLockMode ( alias , convertToLockMode (lockModeType ) );
338
+ }
339
+
286
340
// /**
287
341
// * Set the {@link LockOptions} to use for the whole query.
288
342
// *
@@ -358,6 +412,23 @@ interface Session extends Closeable {
358
412
*/
359
413
<T > Uni <T > find (Class <T > entityClass , Object id , LockMode lockMode );
360
414
415
+ /**
416
+ * Asynchronously return the persistent instance of the given entity
417
+ * class with the given identifier, requesting the given {@link LockModeType}.
418
+ *
419
+ * @param entityClass The entity type
420
+ * @param id an identifier
421
+ * @param lockModeType the requested {@link LockModeType}
422
+ *
423
+ * @return a persistent instance or null via a {@code Uni}
424
+ *
425
+ * @see #find(Class,Object)
426
+ * @see #lock(Object, LockMode) this discussion of lock modes
427
+ */
428
+ default <T > Uni <T > find (Class <T > entityClass , Object id , LockModeType lockModeType ) {
429
+ return find ( entityClass , id , convertToLockMode (lockModeType ) );
430
+ }
431
+
361
432
// /**
362
433
// * Asynchronously return the persistent instance of the given entity
363
434
// * class with the given identifier, requesting the given {@link LockOptions}.
@@ -555,6 +626,19 @@ interface Session extends Closeable {
555
626
*/
556
627
Uni <Void > refresh (Object entity , LockMode lockMode );
557
628
629
+ /**
630
+ * Re-read the state of the given instance from the underlying database,
631
+ * requesting the given {@link LockModeType}.
632
+ *
633
+ * @param entity a managed persistent entity instance
634
+ * @param lockModeType the requested lock mode
635
+ *
636
+ * @see #refresh(Object)
637
+ */
638
+ default Uni <Void > refresh (Object entity , LockModeType lockModeType ) {
639
+ return refresh ( entity , convertToLockMode (lockModeType ) );
640
+ }
641
+
558
642
// /**
559
643
// * Re-read the state of the given instance from the underlying database,
560
644
// * requesting the given {@link LockOptions}.
@@ -597,6 +681,32 @@ interface Session extends Closeable {
597
681
*/
598
682
Uni <Void > lock (Object entity , LockMode lockMode );
599
683
684
+ /**
685
+ * Obtain the specified lock level upon the given object. For example,
686
+ * this operation may be used to:
687
+ *
688
+ * <ul>
689
+ * <li>perform a version check with {@link LockModeType#PESSIMISTIC_READ},
690
+ * <li>upgrade to a pessimistic lock with {@link LockModeType#PESSIMISTIC_WRITE},
691
+ * <li>force a version increment with {@link LockModeType#PESSIMISTIC_FORCE_INCREMENT},
692
+ * <li>schedule a version check just before the end of the transaction with
693
+ * {@link LockModeType#OPTIMISTIC}, or
694
+ * <li>schedule a version increment just before the end of the transaction
695
+ * with {@link LockModeType#OPTIMISTIC_FORCE_INCREMENT}.
696
+ * </ul>
697
+ *
698
+ * This operation cascades to associated instances if the association is
699
+ * mapped with {@link org.hibernate.annotations.CascadeType#LOCK}.
700
+ *
701
+ * @param entity a managed persistent instance
702
+ * @param lockModeType the lock level
703
+ *
704
+ * @throws IllegalArgumentException if the given instance is not managed
705
+ */
706
+ default Uni <Void > lock (Object entity , LockModeType lockModeType ) {
707
+ return lock ( entity , convertToLockMode (lockModeType ) );
708
+ }
709
+
600
710
// /**
601
711
// * Obtain the specified lock level upon the given object, with the given
602
712
// * {@link LockOptions}.
@@ -900,6 +1010,19 @@ <R> Query<R> createNativeQuery(String queryString, ResultSetMapping<R> resultSet
900
1010
*/
901
1011
Session setFlushMode (FlushMode flushMode );
902
1012
1013
+ /**
1014
+ * Set the {@link FlushModeType flush mode} for this session.
1015
+ * <p>
1016
+ * The flush mode determines the points at which the session is flushed.
1017
+ * <i>Flushing</i> is the process of synchronizing the underlying persistent
1018
+ * store with persistable state held in memory.
1019
+ *
1020
+ * @param flushModeType the new flush mode
1021
+ */
1022
+ default Session setFlushMode (FlushModeType flushModeType ) {
1023
+ return setFlushMode ( FlushModeTypeHelper .getFlushMode (flushModeType ) );
1024
+ }
1025
+
903
1026
/**
904
1027
* Get the current flush mode for this session.
905
1028
*
@@ -1032,6 +1155,24 @@ <R> Query<R> createNativeQuery(String queryString, ResultSetMapping<R> resultSet
1032
1155
*/
1033
1156
Session setCacheMode (CacheMode cacheMode );
1034
1157
1158
+ /**
1159
+ * Set the {@link CacheStoreMode} for this session.
1160
+ *
1161
+ * @param cacheStoreMode The new cache store mode.
1162
+ */
1163
+ default Session setCacheStoreMode (CacheStoreMode cacheStoreMode ) {
1164
+ return setCacheMode ( interpretCacheMode ( cacheStoreMode , interpretCacheRetrieveMode (getCacheMode ()) ) );
1165
+ }
1166
+
1167
+ /**
1168
+ * Set the {@link CacheRetrieveMode} for this session.
1169
+ *
1170
+ * @param cacheRetrieveMode The new cache retrieve mode.
1171
+ */
1172
+ default Session setCacheRetrieveMode (CacheRetrieveMode cacheRetrieveMode ) {
1173
+ return setCacheMode ( interpretCacheMode ( interpretCacheStoreMode (getCacheMode ()), cacheRetrieveMode ) );
1174
+ }
1175
+
1035
1176
/**
1036
1177
* Get the current cache mode.
1037
1178
*
@@ -1176,6 +1317,21 @@ interface StatelessSession extends Closeable {
1176
1317
*/
1177
1318
<T > Uni <T > get (Class <T > entityClass , Object id , LockMode lockMode );
1178
1319
1320
+ /**
1321
+ * Retrieve a row, obtaining the specified lock mode.
1322
+ *
1323
+ * @param entityClass The class of the entity to retrieve
1324
+ * @param id The id of the entity to retrieve
1325
+ * @param lockModeType The lock mode to apply to the entity
1326
+ *
1327
+ * @return a detached entity instance, via a {@code Uni}
1328
+ *
1329
+ * @see org.hibernate.StatelessSession#get(Class, Serializable, LockMode)
1330
+ */
1331
+ default <T > Uni <T > get (Class <T > entityClass , Object id , LockModeType lockModeType ) {
1332
+ return get ( entityClass , id , convertToLockMode (lockModeType ) );
1333
+ }
1334
+
1179
1335
/**
1180
1336
* Retrieve a row, using the given {@link EntityGraph} as a fetch plan.
1181
1337
*
@@ -1431,6 +1587,18 @@ interface StatelessSession extends Closeable {
1431
1587
*/
1432
1588
Uni <Void > refresh (Object entity , LockMode lockMode );
1433
1589
1590
+ /**
1591
+ * Refresh the entity instance state from the database.
1592
+ *
1593
+ * @param entity The entity to be refreshed.
1594
+ * @param lockModeType The LockMode to be applied.
1595
+ *
1596
+ * @see org.hibernate.StatelessSession#refresh(Object, LockMode)
1597
+ */
1598
+ default Uni <Void > refresh (Object entity , LockModeType lockModeType ) {
1599
+ return refresh ( entity , convertToLockMode (lockModeType ) );
1600
+ }
1601
+
1434
1602
/**
1435
1603
* Asynchronously fetch an association that's configured for lazy loading.
1436
1604
*
0 commit comments