Skip to content

Commit 5937abd

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

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 enum IncludeRemovals implements MultiFindOption {
29+
INCLUDE,
30+
EXCLUDE
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 enum OrderedReturn implements MultiFindOption {
30+
ORDERED,
31+
UNORDERED
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 enum SessionChecking implements MultiFindOption {
28+
ENABLED,
29+
DISABLED
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
@@ -964,6 +964,15 @@ else if ( option instanceof ReadOnlyMode ) {
964964
else if ( option instanceof BatchSize batchSizeOption ) {
965965
batchSize = batchSizeOption.batchSize();
966966
}
967+
else if ( option instanceof SessionChecking sessionChecking ) {
968+
loadAccess.enableSessionCheck( option == sessionChecking.ENABLED );
969+
}
970+
else if ( option instanceof OrderedReturn orderedReturn ) {
971+
loadAccess.enableOrderedReturn( option == orderedReturn.ORDERED );
972+
}
973+
else if ( option instanceof IncludeRemovals includeRemovals ) {
974+
loadAccess.enableReturnOfDeletedEntities( option == includeRemovals.INCLUDE );
975+
}
967976
}
968977
loadAccess.with( lockOptions )
969978
.with( interpretCacheMode( storeMode, retrieveMode ) )

0 commit comments

Comments
 (0)