Skip to content

Commit cdc028f

Browse files
jrenaatsebersole
authored andcommitted
HHH-19829 - Deprecate MultiIdentifierLoadAccess and the Session.byMultipleIds() methods
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
1 parent d468dfa commit cdc028f

19 files changed

+901
-319
lines changed

documentation/src/main/asciidoc/userguide/chapters/pc/PersistenceContext.adoc

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -186,102 +186,31 @@ If you want to load multiple entities by providing their identifiers, calling th
186186
but also inefficient.
187187

188188
While the Jakarta Persistence standard does not support retrieving multiple entities at once, other than running a JPQL or Criteria API query,
189-
Hibernate offers this functionality via the
190-
https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/Session.html#byMultipleIds-java.lang.Class-[`byMultipleIds` method] of the Hibernate `Session`.
191-
192-
The `byMultipleIds` method returns a
193-
https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/MultiIdentifierLoadAccess.html[`MultiIdentifierLoadAccess`]
194-
which you can use to customize the multi-load request.
195-
196-
The `MultiIdentifierLoadAccess` interface provides several methods which you can use to
197-
change the behavior of the multi-load call:
198-
199-
`enableOrderedReturn(boolean enabled)`::
200-
This setting controls whether the returned `List` is ordered and positional in relation to the
201-
incoming ids. If enabled (the default), the return `List` is ordered and
202-
positional relative to the incoming ids. In other words, a request to
203-
`multiLoad([2,1,3])` will return `[Entity#2, Entity#1, Entity#3]`.
204-
+
205-
An important distinction is made here in regards to the handling of
206-
unknown entities depending on this "ordered return" setting.
207-
If enabled, a null is inserted into the `List` at the proper position(s).
208-
If disabled, the nulls are not put into the return List.
209-
+
210-
In other words, consumers of the returned ordered List would need to be able to handle null elements.
211-
`enableSessionCheck(boolean enabled)`::
212-
This setting, which is disabled by default, tells Hibernate to check the first-level cache (a.k.a `Session` or Persistence Context) first and, if the entity is found and already managed by the Hibernate `Session`, the cached entity will be added to the returned `List`, therefore skipping it from being fetched via the multi-load query.
213-
`enableReturnOfDeletedEntities(boolean enabled)`::
214-
This setting instructs Hibernate if the multi-load operation is allowed to return entities that were deleted by the current Persistence Context. A deleted entity is one which has been passed to this
215-
`Session.delete` or `Session.remove` method, but the `Session` was not flushed yet, meaning that the
216-
associated row was not deleted in the database table.
217-
+
218-
The default behavior is to handle them as null in the return (see `enableOrderedReturn`).
219-
When enabled, the result set will contain deleted entities.
220-
When disabled (which is the default behavior), deleted entities are not included in the returning `List`.
221-
`with(LockOptions lockOptions)`::
222-
This setting allows you to pass a given
223-
https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/LockOptions.html[`LockOptions`] mode to the multi-load query.
224-
`with(CacheMode cacheMode)`::
225-
This setting allows you to pass a given
226-
https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/CacheMode.html[`CacheMode`]
227-
strategy so that we can load entities from the second-level cache, therefore skipping the cached entities from being fetched via the multi-load query.
228-
`withBatchSize(int batchSize)`::
229-
This setting allows you to specify a batch size for loading the entities (e.g. how many at a time).
230-
+
231-
The default is to use a batch sizing strategy defined by the `Dialect.getBatchLoadSizingStrategy()` method.
232-
+
233-
Any greater-than-one value here will override that default behavior.
234-
`with(RootGraph<T> graph)`::
235-
The `RootGraph` is a Hibernate extension to the Jakarta Persistence `EntityGraph` contract,
236-
and this method allows you to pass a specific `RootGraph` to the multi-load query
237-
so that it can fetch additional relationships of the current loading entity.
189+
Hibernate offers this functionality via the `Session#findMultiple` methods which accepts a list of identifiers to load and a group of options which control certain behaviors of the loading -
190+
191+
* `ReadOnlyMode` - whether the entities loaded should be marked as read-only.
192+
* `LockMode` (`LockModeType`) - a lock mode to be applied
193+
* `Timeout` - if a pessimistic lock mode is used, a timeout to allow
194+
* `Locking.Scope` (PessimisticLockScope`) - if a pessimistic lock mode is used, what scope should it be applied to
195+
* `Locking.FollowOn` - allow (or not) Hibernate to acquire locks through additional queries if needed
196+
* `CacheMode` (`CacheStoreMode` / `CacheRetrieveMode`) - how second level caching should be used, if at all
197+
* `BatchSize` - how many identifiers should be loaded from the database at once
198+
* `SessionCheckMode` - whether to look into the persistence context to check entity state
199+
* `RemovalsMode` - if `SessionCheckMode` is enabled, how removed entities should be handled
200+
* `OrderingMode` - whether the results should be ordered according to the order of the passed identifiers
238201

239202
Now, assuming we have 3 `Person` entities in the database, we can load all of them with a single call
240203
as illustrated by the following example:
241204

242205
[[tag::pc-by-multiple-ids-example]]
243-
.Loading multiple entities using the `byMultipleIds()` Hibernate API
206+
.Loading multiple entities using the `findMultiple()` Hibernate API
244207
====
245208
246209
[source, java, indent=0]
247210
----
248-
include::{example-dir-pc}/MultiLoadIdTest.java[tags=pc-by-multiple-ids-example]
211+
include::{example-dir-pc}/FindMultipleDocTests.java[tags=pc-find-multiple-example]
249212
----
250-
251-
[source, sql, indent=0]
252-
----
253-
include::{extrasdir}/pc-by-multiple-ids-example.sql[]
254-
----
255-
====
256-
257-
Notice that only one SQL SELECT statement was executed since the second call uses the
258-
https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/MultiIdentifierLoadAccess.html#enableSessionCheck-boolean-[`enableSessionCheck`] method of the `MultiIdentifierLoadAccess`
259-
to instruct Hibernate to skip entities that are already loaded in the current Persistence Context.
260-
261-
If the entities are not available in the current Persistence Context but they could be loaded from the second-level cache, you can use the
262-
https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/MultiIdentifierLoadAccess.html#with-org.hibernate.CacheMode-[`with(CacheMode)`] method of the `MultiIdentifierLoadAccess` object.
263-
264-
[[tag::pc-by-multiple-ids-second-level-cache-example]]
265-
.Loading multiple entities from the second-level cache
266213
====
267-
[source, java, indent=0]
268-
----
269-
include::{example-dir-pc}/MultiLoadIdTest.java[tags=pc-by-multiple-ids-second-level-cache-example]
270-
----
271-
====
272-
273-
In the example above, we first make sure that we clear the second-level cache to demonstrate that
274-
the multi-load query will put the returning entities into the second-level cache.
275-
276-
After executing the first `byMultipleIds` call, Hibernate is going to fetch the requested entities,
277-
and as illustrated by the `getSecondLevelCachePutCount` method call, 3 entities were indeed added to the
278-
shared cache.
279-
280-
Afterward, when executing the second `byMultipleIds` call for the same entities in a new Hibernate `Session`,
281-
we set the
282-
https://docs.jboss.org/hibernate/orm/{majorMinorVersion}/javadocs/org/hibernate/CacheMode.html#NORMAL[`CacheMode.NORMAL`] second-level cache mode so that entities are going to be returned from the second-level cache.
283-
284-
The `getSecondLevelCacheHitCount` statistics method returns 3 this time, since the 3 entities were loaded from the second-level cache, and, as illustrated by `sqlStatementInterceptor.getSqlQueries()`, no multi-load SELECT statement was executed this time.
285214

286215
[[pc-find-natural-id]]
287216
=== Obtain an entity by natural-id
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate;
6+
7+
8+
import jakarta.persistence.EntityGraph;
9+
import jakarta.persistence.FindOption;
10+
11+
import java.util.List;
12+
13+
/**
14+
* Simple marker interface for FindOptions which can be applied to multiple id loading.
15+
*
16+
* @see org.hibernate.Session#findMultiple(Class, List, FindOption...)
17+
* @see org.hibernate.Session#findMultiple(EntityGraph, List , FindOption...)
18+
*
19+
* @since 7.2
20+
*/
21+
public interface FindMultipleOption extends FindOption {
22+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
* @see Session#byMultipleIds(Class)
3232
*
3333
* @author Steve Ebersole
34+
35+
* @deprecated Use forms of {@linkplain Session#findMultiple} accepting
36+
* {@linkplain jakarta.persistence.FindOption} instead of {@linkplain Session#byMultipleIds}.
3437
*/
38+
@Deprecated(since = "7.2", forRemoval = true)
3539
public interface MultiIdentifierLoadAccess<T> {
3640

3741
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate;
6+
7+
8+
import jakarta.persistence.EntityGraph;
9+
import jakarta.persistence.FindOption;
10+
11+
import java.util.List;
12+
13+
/**
14+
* Indicates whether the result list should be ordered relative to the
15+
* position of the identifier list. E.g.
16+
* <pre>
17+
* List&lt;Person&gt; results = session.findMultiple(
18+
* Person.class,
19+
* List.of(1,2,3,2),
20+
* ORDERED
21+
* );
22+
* assert results.get(0).getId() == 1;
23+
* assert results.get(1).getId() == 2;
24+
* assert results.get(2).getId() == 3;
25+
* assert results.get(3).getId() == 2;
26+
* </pre>
27+
* <p>
28+
* The default is {@link #ORDERED}.
29+
*
30+
* @see org.hibernate.Session#findMultiple(Class, List, FindOption...)
31+
* @see org.hibernate.Session#findMultiple(EntityGraph, List , FindOption...)
32+
*
33+
* @since 7.2
34+
*/
35+
@Incubating
36+
public enum OrderingMode implements FindMultipleOption {
37+
/**
38+
* The default. The result list is ordered relative to the
39+
* position of the identifiers list.
40+
*
41+
* @see RemovalsMode
42+
*/
43+
ORDERED,
44+
/**
45+
* The result list may be in any order.
46+
*/
47+
UNORDERED
48+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate;
6+
7+
8+
import jakarta.persistence.EntityGraph;
9+
import jakarta.persistence.FindOption;
10+
11+
import java.util.List;
12+
13+
/**
14+
* When {@linkplain SessionCheckMode} is enabled, this option controls how
15+
* to handle entities which are already contained by the persistence context
16+
* but which are in a removed state (marked for removal, but not yet flushed).
17+
* <p>
18+
* The default is {@link #REPLACE}.
19+
*
20+
* @see org.hibernate.Session#findMultiple(Class, List, FindOption...)
21+
* @see org.hibernate.Session#findMultiple(EntityGraph, List , FindOption...)
22+
*
23+
* @since 7.2
24+
*/
25+
@Incubating
26+
public enum RemovalsMode implements FindMultipleOption {
27+
/**
28+
* Removed entities are included in the load result.
29+
*/
30+
INCLUDE,
31+
/**
32+
* The default. Removed entities are replaced with {@code null} in the load result.
33+
*/
34+
REPLACE
35+
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,6 @@ public interface Session extends SharedSessionContract, EntityManager {
577577
* <li>on the other hand, for databases with no SQL array type, a large batch size results
578578
* in long SQL statements with many JDBC parameters.
579579
* </ul>
580-
* <p>
581-
* For more advanced cases, use {@link #byMultipleIds(Class)}, which returns an instance of
582-
* {@link MultiIdentifierLoadAccess}.
583580
*
584581
* @param entityType the entity type
585582
* @param ids the list of identifiers
@@ -588,7 +585,9 @@ public interface Session extends SharedSessionContract, EntityManager {
588585
* @return an ordered list of persistent instances, with null elements representing missing
589586
* entities, whose positions in the list match the positions of their ids in the
590587
* given list of identifiers
591-
* @see #byMultipleIds(Class)
588+
*
589+
* @see FindMultipleOption
590+
*
592591
* @since 7.0
593592
*/
594593
<E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption... options);
@@ -617,9 +616,6 @@ public interface Session extends SharedSessionContract, EntityManager {
617616
* <li>on the other hand, for databases with no SQL array type, a large batch size results
618617
* in long SQL statements with many JDBC parameters.
619618
* </ul>
620-
* <p>
621-
* For more advanced cases, use {@link #byMultipleIds(Class)}, which returns an instance of
622-
* {@link MultiIdentifierLoadAccess}.
623619
*
624620
* @param entityGraph the entity graph interpreted as a load graph
625621
* @param ids the list of identifiers
@@ -628,7 +624,9 @@ public interface Session extends SharedSessionContract, EntityManager {
628624
* @return an ordered list of persistent instances, with null elements representing missing
629625
* entities, whose positions in the list match the positions of their ids in the
630626
* given list of identifiers
631-
* @see #byMultipleIds(Class)
627+
*
628+
* @see FindMultipleOption
629+
*
632630
* @since 7.0
633631
*/
634632
<E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options);
@@ -1165,7 +1163,10 @@ public interface Session extends SharedSessionContract, EntityManager {
11651163
* @throws HibernateException If the given class does not resolve as a mapped entity
11661164
*
11671165
* @see #findMultiple(Class, List, FindOption...)
1166+
*
1167+
* @deprecated Use {@link #findMultiple(Class, List, FindOption...)} instead.
11681168
*/
1169+
@Deprecated(since = "7.2", forRemoval = true)
11691170
<T> MultiIdentifierLoadAccess<T> byMultipleIds(Class<T> entityClass);
11701171

11711172
/**
@@ -1177,7 +1178,11 @@ public interface Session extends SharedSessionContract, EntityManager {
11771178
* @return an instance of {@link MultiIdentifierLoadAccess} for executing the lookup
11781179
*
11791180
* @throws HibernateException If the given name does not resolve to a mapped entity
1181+
*
1182+
* @deprecated Use {@link #findMultiple(EntityGraph, List, FindOption...)} instead,
1183+
* with {@linkplain SessionFactory#createGraphForDynamicEntity(String)}.
11801184
*/
1185+
@Deprecated(since = "7.2", forRemoval = true)
11811186
<T> MultiIdentifierLoadAccess<T> byMultipleIds(String entityName);
11821187

11831188
/**
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate;
6+
7+
8+
import jakarta.persistence.EntityGraph;
9+
import jakarta.persistence.FindOption;
10+
11+
import java.util.List;
12+
13+
/**
14+
* Indicates whether the persistence context should be checked for entities
15+
* matching the identifiers to be loaded - <ul>
16+
* <li>Entities which are in a managed state are not re-loaded from the database.
17+
* those identifiers are removed from the SQL restriction sent to the database.
18+
* <li>Entities which are in a removed state are {@linkplain RemovalsMode#REPLACE excluded}
19+
* from the result by default, but can be {@linkplain RemovalsMode#INCLUDE included} if desired.
20+
* </ul>
21+
* <p/>
22+
* The default is {@link #DISABLED}
23+
*
24+
* @see org.hibernate.Session#findMultiple(Class, List , FindOption...)
25+
* @see org.hibernate.Session#findMultiple(EntityGraph, List , FindOption...)
26+
*
27+
* @since 7.2
28+
*/
29+
@Incubating
30+
public enum SessionCheckMode implements FindMultipleOption {
31+
/**
32+
* The persistence context will be checked. Identifiers for entities already contained
33+
* in the persistence context will not be sent to the database for loading. If the
34+
* entity is marked for removal in the persistence context, whether it is returned
35+
* is controlled by {@linkplain RemovalsMode}.
36+
*
37+
* @see RemovalsMode
38+
*/
39+
ENABLED,
40+
/**
41+
* The default. All identifiers to be loaded will be read from the database and returned.
42+
*/
43+
DISABLED
44+
}

0 commit comments

Comments
 (0)