Skip to content

Commit e868c8c

Browse files
committed
HHH-18748 Add missing overloads of find() to Session
It was always weird and inconsistent that you sometimes had to use get(), and this is also better documented this way (i.e. Hibernate-specific semantics of find() are not documented by EntityManager) Also add some missing @overload annotations Also move some impl down off the interface Signed-off-by: Gavin King <[email protected]>
1 parent 5fca525 commit e868c8c

File tree

7 files changed

+216
-125
lines changed

7 files changed

+216
-125
lines changed

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

Lines changed: 118 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
import jakarta.persistence.CacheRetrieveMode;
1717
import jakarta.persistence.CacheStoreMode;
18-
import jakarta.persistence.ConnectionConsumer;
19-
import jakarta.persistence.ConnectionFunction;
2018
import jakarta.persistence.EntityGraph;
2119
import jakarta.persistence.EntityManager;
2220
import jakarta.persistence.FlushModeType;
@@ -194,6 +192,7 @@ public interface Session extends SharedSessionContract, EntityManager {
194192
*
195193
* @throws HibernateException if changes could not be synchronized with the database
196194
*/
195+
@Override
197196
void flush();
198197

199198
/**
@@ -205,7 +204,7 @@ public interface Session extends SharedSessionContract, EntityManager {
205204
*
206205
* @param flushMode the new {@link FlushModeType}
207206
*
208-
* @see #setHibernateFlushMode(FlushMode) for additional options
207+
* @see #setHibernateFlushMode(FlushMode)
209208
*/
210209
@Override
211210
void setFlushMode(FlushModeType flushMode);
@@ -232,6 +231,8 @@ public interface Session extends SharedSessionContract, EntityManager {
232231
* Get the current {@linkplain FlushModeType JPA flush mode} for this session.
233232
*
234233
* @return the {@link FlushModeType} currently in effect
234+
*
235+
* @see #getHibernateFlushMode()
235236
*/
236237
@Override
237238
FlushModeType getFlushMode();
@@ -267,6 +268,7 @@ public interface Session extends SharedSessionContract, EntityManager {
267268
*
268269
* @since 6.2
269270
*/
271+
@Override
270272
CacheStoreMode getCacheStoreMode();
271273

272274
/**
@@ -276,6 +278,7 @@ public interface Session extends SharedSessionContract, EntityManager {
276278
*
277279
* @since 6.2
278280
*/
281+
@Override
279282
CacheRetrieveMode getCacheRetrieveMode();
280283

281284
/**
@@ -287,6 +290,7 @@ public interface Session extends SharedSessionContract, EntityManager {
287290
*
288291
* @since 6.2
289292
*/
293+
@Override
290294
void setCacheStoreMode(CacheStoreMode cacheStoreMode);
291295

292296
/**
@@ -298,6 +302,7 @@ public interface Session extends SharedSessionContract, EntityManager {
298302
*
299303
* @since 6.2
300304
*/
305+
@Override
301306
void setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode);
302307

303308
/**
@@ -474,9 +479,109 @@ public interface Session extends SharedSessionContract, EntityManager {
474479
*/
475480
void evict(Object object);
476481

482+
/**
483+
* Return the persistent instance of the given entity class with the given identifier,
484+
* or null if there is no such persistent instance. If the instance is already associated
485+
* with the session, return that instance. This method never returns an uninitialized
486+
* instance.
487+
* <p>
488+
* The object returned by {@code get()} or {@code find()} is either an unproxied instance
489+
* of the given entity class, or a fully-fetched proxy object.
490+
* <p>
491+
* This operation requests {@link LockMode#NONE}, that is, no lock, allowing the object
492+
* to be retrieved from the cache without the cost of database access. However, if it is
493+
* necessary to read the state from the database, the object will be returned with the
494+
* lock mode {@link LockMode#READ}.
495+
* <p>
496+
* To bypass the {@linkplain Cache second-level cache}, and ensure that the state of the
497+
* requested instance is read directly from the database, either:
498+
* <ul>
499+
* <li>call {@link #find(Class, Object, FindOption...)}, passing
500+
* {@link CacheRetrieveMode#BYPASS} as an option,
501+
* <li>call {@link #find(Class, Object, LockMode)} with the explicit lock mode
502+
* {@link LockMode#READ}, or
503+
* <li>{@linkplain #setCacheRetrieveMode set the cache mode} to
504+
* {@link CacheRetrieveMode#BYPASS} before calling this method.
505+
* </ul>
506+
*
507+
* @apiNote This operation is very similar to {@link #get(Class, Object)}.
508+
*
509+
* @param entityType the entity type
510+
* @param id an identifier
511+
*
512+
* @return a fully-fetched persistent instance or null
513+
*/
514+
@Override
515+
<T> T find(Class<T> entityType, Object id);
516+
517+
/**
518+
* Return the persistent instance of the given entity class with the given identifier,
519+
* or null if there is no such persistent instance. If the instance is already associated
520+
* with the session, return that instance. This method never returns an uninitialized
521+
* instance. Obtain the specified lock mode if the instance exists.
522+
* <p>
523+
* Convenient form of {@link #find(Class, Object, LockOptions)}.
524+
*
525+
* @param entityType the entity type
526+
* @param id an identifier
527+
* @param lockMode the lock mode
528+
*
529+
* @return a fully-fetched persistent instance or null
530+
*
531+
* @since 7.0
532+
*
533+
* @see #find(Class, Object, LockOptions)
534+
*/
535+
<T> T find(Class<T> entityType, Object id, LockMode lockMode);
536+
537+
/**
538+
* Return the persistent instance of the given entity class with the given identifier,
539+
* or null if there is no such persistent instance. If the instance is already associated
540+
* with the session, return that instance. This method never returns an uninitialized
541+
* instance. Obtain the specified lock mode if the instance exists.
542+
*
543+
* @param entityType the entity type
544+
* @param id an identifier
545+
* @param lockOptions the lock mode
546+
*
547+
* @return a fully-fetched persistent instance or null
548+
*
549+
* @since 7.0
550+
*/
551+
<T> T find(Class<T> entityType, Object id, LockOptions lockOptions);
552+
553+
/**
554+
* Return the persistent instances of the given entity class with the given identifiers
555+
* as a list. The position of an instance in the returned list matches the position of its
556+
* identifier in the given list of identifiers, and the returned list contains a null value
557+
* if there is no persistent instance matching a given identifier. If an instance is already
558+
* associated with the session, that instance is returned. This method never returns an
559+
* uninitialized instance.
560+
* <p>
561+
* Every object returned by {@code findMultiple()} is either an unproxied instance of the
562+
* given entity class, or a fully-fetched proxy object.
563+
* <p>
564+
* For more advanced cases, use {@link #byMultipleIds(Class)}, which returns an instance of
565+
* {@link MultiIdentifierLoadAccess}.
566+
*
567+
* @param entityType the entity type
568+
* @param ids the list of identifiers
569+
* @param options options, if any
570+
*
571+
* @return an ordered list of persistent instances, with null elements representing missing
572+
* entities, whose positions in the list match the positions of their ids in the
573+
* given list of identifiers
574+
* @see #byMultipleIds(Class)
575+
* @since 7.0
576+
*/
577+
<E> List<E> findMultiple(Class<E> entityType, List<Object> ids, FindOption... options);
578+
477579
/**
478580
* Read the persistent state associated with the given identifier into the given
479581
* transient instance.
582+
*
583+
* @param object a transient instance of an entity class
584+
* @param id an identifier
480585
*/
481586
void load(Object object, Object id);
482587

@@ -524,6 +629,7 @@ public interface Session extends SharedSessionContract, EntityManager {
524629
*
525630
* @return an updated persistent instance
526631
*/
632+
@Override
527633
<T> T merge(T object);
528634

529635
/**
@@ -554,6 +660,7 @@ public interface Session extends SharedSessionContract, EntityManager {
554660
*
555661
* @param object a transient instance to be made persistent
556662
*/
663+
@Override
557664
void persist(Object object);
558665

559666
/**
@@ -635,6 +742,7 @@ public interface Session extends SharedSessionContract, EntityManager {
635742
*
636743
* @param object a persistent instance associated with this session
637744
*/
745+
@Override
638746
void refresh(Object object);
639747

640748
/**
@@ -688,39 +796,15 @@ public interface Session extends SharedSessionContract, EntityManager {
688796
* saves, updates and deletions. Do not close open iterators or instances of
689797
* {@link ScrollableResults}.
690798
*/
799+
@Override
691800
void clear();
692801

693-
/**
694-
* Return the persistent instances of the given entity class with the given identifiers
695-
* as a list. The position of an instance in the list matches the position of its identifier
696-
* in the given array, and the list contains a null value if there is no persistent instance
697-
* matching a given identifier. If an instance is already associated with the session, that
698-
* instance is returned. This method never returns an uninitialized instance.
699-
* <p>
700-
* Every object returned by {@code findMultiple()} is either an unproxied instance of the
701-
* given entity class, or a fully-fetched proxy object.
702-
* <p>
703-
* For more advanced cases, use {@link #byMultipleIds(Class)}, which returns an instance of
704-
* {@link MultiIdentifierLoadAccess}.
705-
*
706-
* @param entityType the entity type
707-
* @param ids the identifiers
708-
* @param options options, if any
709-
* @return an ordered list of persistent instances, with null elements representing missing
710-
* entities
711-
* @see #byMultipleIds(Class)
712-
* @since 7.0
713-
*/
714-
<E> List<E> findMultiple(Class<E> entityType, List<Object> ids, FindOption... options);
715-
716802
/**
717803
* Return the persistent instance of the given entity class with the given identifier,
718804
* or null if there is no such persistent instance. If the instance is already associated
719805
* with the session, return that instance. This method never returns an uninitialized
720806
* instance.
721807
* <p>
722-
* This operation is very similar to {@link #find(Class, Object)}.
723-
* <p>
724808
* The object returned by {@code get()} or {@code find()} is either an unproxied instance
725809
* of the given entity class, or a fully-fetched proxy object.
726810
* <p>
@@ -734,10 +818,12 @@ public interface Session extends SharedSessionContract, EntityManager {
734818
* <ul>
735819
* <li>call {@link #get(Class, Object, LockMode)} with the explicit lock mode
736820
* {@link LockMode#READ}, or
737-
* <li>{@linkplain #setCacheMode(CacheMode) set the cache mode} to {@link CacheMode#IGNORE}
821+
* <li>{@linkplain #setCacheMode set the cache mode} to {@link CacheMode#IGNORE}
738822
* before calling this method.
739823
* </ul>
740824
*
825+
* @apiNote This operation is very similar to {@link #find(Class, Object)}.
826+
*
741827
* @param entityType the entity type
742828
* @param id an identifier
743829
*
@@ -752,8 +838,8 @@ public interface Session extends SharedSessionContract, EntityManager {
752838
* instance. Obtain the specified lock mode if the instance exists.
753839
* <p>
754840
* Convenient form of {@link #get(Class, Object, LockOptions)}.
755-
* <p>
756-
* This operation is very similar to {@link #find(Class, Object, jakarta.persistence.LockModeType)}.
841+
*
842+
* @apiNote This operation is very similar to {@link #find(Class, Object, LockModeType)}.
757843
*
758844
* @param entityType the entity type
759845
* @param id an identifier
@@ -886,6 +972,7 @@ public interface Session extends SharedSessionContract, EntityManager {
886972
*
887973
* @since 6.0
888974
*/
975+
@Override
889976
<T> T getReference(T object);
890977

891978
/**
@@ -1026,15 +1113,6 @@ public interface Session extends SharedSessionContract, EntityManager {
10261113
*/
10271114
<T> NaturalIdMultiLoadAccess<T> byMultipleNaturalId(String entityName);
10281115

1029-
@Override
1030-
Filter enableFilter(String filterName);
1031-
1032-
@Override
1033-
Filter getEnabledFilter(String filterName);
1034-
1035-
@Override
1036-
void disableFilter(String filterName);
1037-
10381116
/**
10391117
* Get the {@linkplain SessionStatistics statistics} for this session.
10401118
*
@@ -1196,32 +1274,4 @@ public interface Session extends SharedSessionContract, EntityManager {
11961274
*/
11971275
@Override @Deprecated(since = "6.0") @SuppressWarnings("rawtypes")
11981276
Query createQuery(CriteriaUpdate updateQuery);
1199-
1200-
1201-
@Override
1202-
default <C> void runWithConnection(ConnectionConsumer<C> action) {
1203-
doWork( connection -> {
1204-
try {
1205-
//noinspection unchecked
1206-
action.accept( (C) connection );
1207-
}
1208-
catch (Exception e) {
1209-
throw new RuntimeException( e );
1210-
}
1211-
} );
1212-
}
1213-
1214-
@Override
1215-
default <C, T> T callWithConnection(ConnectionFunction<C, T> function) {
1216-
return doReturningWork( (connection) -> {
1217-
try {
1218-
//noinspection unchecked
1219-
return function.apply( (C) connection );
1220-
}
1221-
catch (Exception e) {
1222-
throw new RuntimeException( e );
1223-
}
1224-
} );
1225-
}
1226-
12271277
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ default <R> R fromStatelessTransaction(Function<StatelessSession,R> action) {
302302
*
303303
* @since 6.2
304304
*/
305+
@Override
305306
SchemaManager getSchemaManager();
306307

307308
/**
@@ -327,6 +328,7 @@ default <R> R fromStatelessTransaction(Function<StatelessSession,R> action) {
327328
*
328329
* @throws HibernateException Indicates an issue closing the factory.
329330
*/
331+
@Override
330332
void close() throws HibernateException;
331333

332334
/**

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionDelegatorBaseImpl.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,16 @@ public <T> T find(EntityGraph<T> entityGraph, Object primaryKey, FindOption... o
871871
return delegate.find( entityGraph, primaryKey, options );
872872
}
873873

874+
@Override
875+
public <T> T find(Class<T> entityType, Object id, LockMode lockMode) {
876+
return delegate.find( entityType, id, lockMode );
877+
}
878+
879+
@Override
880+
public <T> T find(Class<T> entityType, Object id, LockOptions lockOptions) {
881+
return delegate.find( entityType, id, lockOptions );
882+
}
883+
874884
@Override
875885
public <T> T getReference(Class<T> entityClass, Object id) {
876886
return delegate.getReference( entityClass, id );

hibernate-core/src/main/java/org/hibernate/engine/spi/SessionImplementor.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55
package org.hibernate.engine.spi;
66

7+
import jakarta.persistence.ConnectionConsumer;
8+
import jakarta.persistence.ConnectionFunction;
79
import org.hibernate.HibernateException;
810
import org.hibernate.LockOptions;
911
import org.hibernate.Session;
@@ -108,4 +110,29 @@ default boolean isSessionImplementor() {
108110
return true;
109111
}
110112

113+
@Override
114+
default <C> void runWithConnection(ConnectionConsumer<C> action) {
115+
doWork( connection -> {
116+
try {
117+
//noinspection unchecked
118+
action.accept( (C) connection );
119+
}
120+
catch (Exception e) {
121+
throw new RuntimeException( e );
122+
}
123+
} );
124+
}
125+
126+
@Override
127+
default <C, T> T callWithConnection(ConnectionFunction<C, T> function) {
128+
return doReturningWork( connection -> {
129+
try {
130+
//noinspection unchecked
131+
return function.apply( (C) connection );
132+
}
133+
catch (Exception e) {
134+
throw new RuntimeException( e );
135+
}
136+
} );
137+
}
111138
}

0 commit comments

Comments
 (0)