You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: documentation/src/main/asciidoc/userguide/chapters/pc/PersistenceContext.adoc
+14-85Lines changed: 14 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -186,102 +186,31 @@ If you want to load multiple entities by providing their identifiers, calling th
186
186
but also inefficient.
187
187
188
188
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`.
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.
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.
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
238
201
239
202
Now, assuming we have 3 `Person` entities in the database, we can load all of them with a single call
240
203
as illustrated by the following example:
241
204
242
205
[[tag::pc-by-multiple-ids-example]]
243
-
.Loading multiple entities using the `byMultipleIds()` Hibernate API
206
+
.Loading multiple entities using the `findMultiple()` Hibernate API
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.
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.
0 commit comments