Skip to content

Commit c572e6d

Browse files
gavinkingDavideD
authored andcommitted
add API support for CacheRetrieveMode, CacheStoreMode, LockModeType, FlushModeType
fixes #600
1 parent 78f35e2 commit c572e6d

File tree

2 files changed

+336
-0
lines changed

2 files changed

+336
-0
lines changed

hibernate-reactive-core/src/main/java/org/hibernate/reactive/mutiny/Mutiny.java

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import java.util.List;
1111
import java.util.function.BiFunction;
1212
import java.util.function.Function;
13+
import javax.persistence.CacheRetrieveMode;
14+
import javax.persistence.CacheStoreMode;
1315
import javax.persistence.EntityGraph;
16+
import javax.persistence.FlushModeType;
17+
import javax.persistence.LockModeType;
1418
import javax.persistence.Parameter;
1519
import javax.persistence.criteria.CriteriaBuilder;
1620
import javax.persistence.criteria.CriteriaDelete;
@@ -31,6 +35,7 @@
3135
import org.hibernate.engine.spi.PersistentAttributeInterceptable;
3236
import org.hibernate.engine.spi.PersistentAttributeInterceptor;
3337
import org.hibernate.engine.spi.SharedSessionContractImplementor;
38+
import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
3439
import org.hibernate.proxy.HibernateProxy;
3540
import org.hibernate.reactive.common.AffectedEntities;
3641
import org.hibernate.reactive.common.Identifier;
@@ -42,6 +47,11 @@
4247
import io.smallrye.mutiny.Uni;
4348
import org.hibernate.stat.Statistics;
4449

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+
4555
/**
4656
* An API for Hibernate Reactive where non-blocking operations are
4757
* represented by a Mutiny {@link Uni}.
@@ -243,6 +253,22 @@ interface Query<R> {
243253
*/
244254
Query<R> setCacheMode(CacheMode cacheMode);
245255

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+
246272
/**
247273
* Obtain the {@link CacheMode} in effect for this query. By default,
248274
* the query inherits the {@code CacheMode} of the {@link Session}
@@ -258,6 +284,14 @@ interface Query<R> {
258284
*/
259285
Query<R> setFlushMode(FlushMode flushMode);
260286

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+
}
261295
/**
262296
* Obtain the {@link FlushMode} in effect for this query. By default,
263297
* the query inherits the {@code FlushMode} of the {@link Session}
@@ -272,6 +306,13 @@ interface Query<R> {
272306
*/
273307
Query<R> setLockMode(LockMode lockMode);
274308

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+
275316
/**
276317
* Set the {@link LockMode} to use for specified alias (as defined in
277318
* the query's {@code from} clause).
@@ -283,6 +324,19 @@ interface Query<R> {
283324
*/
284325
Query<R> setLockMode(String alias, LockMode lockMode);
285326

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+
286340
// /**
287341
// * Set the {@link LockOptions} to use for the whole query.
288342
// *
@@ -358,6 +412,23 @@ interface Session extends Closeable {
358412
*/
359413
<T> Uni<T> find(Class<T> entityClass, Object id, LockMode lockMode);
360414

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+
361432
// /**
362433
// * Asynchronously return the persistent instance of the given entity
363434
// * class with the given identifier, requesting the given {@link LockOptions}.
@@ -555,6 +626,19 @@ interface Session extends Closeable {
555626
*/
556627
Uni<Void> refresh(Object entity, LockMode lockMode);
557628

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+
558642
// /**
559643
// * Re-read the state of the given instance from the underlying database,
560644
// * requesting the given {@link LockOptions}.
@@ -597,6 +681,32 @@ interface Session extends Closeable {
597681
*/
598682
Uni<Void> lock(Object entity, LockMode lockMode);
599683

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+
600710
// /**
601711
// * Obtain the specified lock level upon the given object, with the given
602712
// * {@link LockOptions}.
@@ -900,6 +1010,19 @@ <R> Query<R> createNativeQuery(String queryString, ResultSetMapping<R> resultSet
9001010
*/
9011011
Session setFlushMode(FlushMode flushMode);
9021012

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+
9031026
/**
9041027
* Get the current flush mode for this session.
9051028
*
@@ -1032,6 +1155,24 @@ <R> Query<R> createNativeQuery(String queryString, ResultSetMapping<R> resultSet
10321155
*/
10331156
Session setCacheMode(CacheMode cacheMode);
10341157

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+
10351176
/**
10361177
* Get the current cache mode.
10371178
*
@@ -1176,6 +1317,21 @@ interface StatelessSession extends Closeable {
11761317
*/
11771318
<T> Uni<T> get(Class<T> entityClass, Object id, LockMode lockMode);
11781319

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+
11791335
/**
11801336
* Retrieve a row, using the given {@link EntityGraph} as a fetch plan.
11811337
*
@@ -1431,6 +1587,18 @@ interface StatelessSession extends Closeable {
14311587
*/
14321588
Uni<Void> refresh(Object entity, LockMode lockMode);
14331589

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+
14341602
/**
14351603
* Asynchronously fetch an association that's configured for lazy loading.
14361604
*

0 commit comments

Comments
 (0)