Skip to content

Commit 11c6f4d

Browse files
committed
HHH-19829 - Introduce MultiFindOptions and its implementations for use in findMultiple()
Signed-off-by: Jan Schatteman <[email protected]>
1 parent a5dc413 commit 11c6f4d

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
* MultiFindOption implementation to specify whether the returned list
15+
* of entity instances should contain instances that have been
16+
* {@linkplain Session#remove(Object) marked for removal} in the
17+
* current session, but not yet deleted in the database.
18+
* <p>
19+
* The default is {@link #EXCLUDE}, meaning that instances marked for
20+
* removal are replaced by null in the returned list of entities when {@link OrderedReturn}
21+
* is used.
22+
*
23+
* @see org.hibernate.MultiFindOption
24+
* @see OrderedReturn
25+
* @see org.hibernate.Session#findMultiple(Class, List, FindOption...)
26+
* @see org.hibernate.Session#findMultiple(EntityGraph, List , FindOption...)
27+
*/
28+
public record IncludeRemovals(boolean include) implements MultiFindOption {
29+
public static IncludeRemovals INCLUDE = new IncludeRemovals( true );
30+
public static IncludeRemovals EXCLUDE = new IncludeRemovals( false );
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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.FindOption;
9+
10+
/**
11+
* Simple marker interface for FindOptions which can be applied to multiple id loading.
12+
*/
13+
interface MultiFindOption extends FindOption {
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
* MultiFindOption implementation to specify whether the returned list
15+
* of entity instances should be ordered, where the position of an entity
16+
* instance is determined by the position of its identifier
17+
* in the list of ids passed to {@code findMultiple(...)}.
18+
* <p>
19+
* The default is {@link #ORDERED}, meaning the positions of the entities
20+
* in the returned list correspond to the positions of their ids. In this case,
21+
* the {@link IncludeRemovals} handling of entities marked for removal
22+
* becomes important.
23+
*
24+
* @see org.hibernate.MultiFindOption
25+
* @see IncludeRemovals
26+
* @see org.hibernate.Session#findMultiple(Class, List, FindOption...)
27+
* @see org.hibernate.Session#findMultiple(EntityGraph, List , FindOption...)
28+
*/
29+
public record OrderedReturn(boolean ordered) implements MultiFindOption {
30+
public static OrderedReturn ORDERED = new OrderedReturn( true );
31+
public static OrderedReturn UNORDERED = new OrderedReturn( false );
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
* MultiFindOption implementation to specify whether the ids of managed entity instances already
15+
* cached in the current persistence context should be excluded.
16+
* from the list of ids sent to the database.
17+
* <p>
18+
* The default is {@link #DISABLED}, meaning all ids are included and sent to the database.
19+
*
20+
* Use {@link #ENABLED} to exclude already managed entity instance ids from
21+
* the list of ids sent to the database.
22+
*
23+
* @see org.hibernate.MultiFindOption
24+
* @see org.hibernate.Session#findMultiple(Class, List , FindOption...)
25+
* @see org.hibernate.Session#findMultiple(EntityGraph, List , FindOption...)
26+
*/
27+
public record SessionChecking(boolean enabled) implements MultiFindOption {
28+
public static SessionChecking ENABLED = new SessionChecking( true );
29+
public static SessionChecking DISABLED = new SessionChecking( false );
30+
}

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,15 @@ else if ( option instanceof ReadOnlyMode ) {
960960
else if ( option instanceof BatchSize batchSizeOption ) {
961961
batchSize = batchSizeOption.batchSize();
962962
}
963+
else if ( option instanceof SessionChecking sessionChecking ) {
964+
loadAccess.enableSessionCheck( sessionChecking.enabled() );
965+
}
966+
else if ( option instanceof OrderedReturn orderedReturn ) {
967+
loadAccess.enableOrderedReturn( orderedReturn.ordered() );
968+
}
969+
else if ( option instanceof IncludeRemovals includeRemovals ) {
970+
loadAccess.enableReturnOfDeletedEntities( includeRemovals.include() );
971+
}
963972
}
964973
loadAccess.with( lockOptions )
965974
.with( interpretCacheMode( storeMode, retrieveMode ) )

0 commit comments

Comments
 (0)