Skip to content

Commit 0530f02

Browse files
jrenaatsebersole
authored andcommitted
HHH-19829 - Deprecate MultiIdentifierLoadAccess and the Session.byMultipleIds() methods
Signed-off-by: Jan Schatteman <[email protected]>
1 parent c17cb96 commit 0530f02

File tree

9 files changed

+561
-177
lines changed

9 files changed

+561
-177
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+
public interface MultiFindOption extends FindOption {
14+
}

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: 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+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,12 @@ public interface Session extends SharedSessionContract, EntityManager {
11651165
* @throws HibernateException If the given class does not resolve as a mapped entity
11661166
*
11671167
* @see #findMultiple(Class, List, FindOption...)
1168+
*
1169+
* @deprecated This method will be removed.
1170+
* Use {@link #findMultiple(Class, List, FindOption...)} instead.
1171+
* See {@link MultiFindOption}.
11681172
*/
1173+
@Deprecated(since = "7.2", forRemoval = true)
11691174
<T> MultiIdentifierLoadAccess<T> byMultipleIds(Class<T> entityClass);
11701175

11711176
/**
@@ -1177,7 +1182,12 @@ public interface Session extends SharedSessionContract, EntityManager {
11771182
* @return an instance of {@link MultiIdentifierLoadAccess} for executing the lookup
11781183
*
11791184
* @throws HibernateException If the given name does not resolve to a mapped entity
1185+
*
1186+
* @deprecated This method will be removed.
1187+
* Use {@link #findMultiple(Class, List, FindOption...)} instead.
1188+
* See {@link MultiFindOption}.
11801189
*/
1190+
@Deprecated(since = "7.2", forRemoval = true)
11811191
<T> MultiIdentifierLoadAccess<T> byMultipleIds(String entityName);
11821192

11831193
/**
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: 12 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 ) )
@@ -2291,6 +2300,9 @@ else if ( option instanceof EnabledFetchProfile enabledFetchProfile ) {
22912300
else if ( option instanceof ReadOnlyMode ) {
22922301
loadAccess.withReadOnly( option == ReadOnlyMode.READ_ONLY );
22932302
}
2303+
else if ( option instanceof MultiFindOption multiFindOption ) {
2304+
throw new IllegalArgumentException( "Option '" + multiFindOption + "' can only be used in 'findMultiple()'" );
2305+
}
22942306
}
22952307
if ( lockOptions.getLockMode().isPessimistic() ) {
22962308
if ( lockOptions.getTimeOut() == WAIT_FOREVER_MILLI ) {

hibernate-core/src/test/java/org/hibernate/orm/test/dynamicmap/FindOperationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
*/
55
package org.hibernate.orm.test.dynamicmap;
66

7+
import org.hibernate.IncludeRemovals;
8+
import org.hibernate.OrderedReturn;
79
import org.hibernate.ReadOnlyMode;
10+
import org.hibernate.SessionChecking;
811
import org.hibernate.graph.RootGraph;
912
import org.hibernate.testing.orm.junit.DomainModel;
1013
import org.hibernate.testing.orm.junit.SessionFactory;
@@ -17,6 +20,7 @@
1720
import java.util.Map;
1821

1922
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
2024

2125
/**
2226
* @author Steve Ebersole
@@ -58,6 +62,15 @@ void testFindWithOptions(SessionFactoryScope factoryScope) {
5862
} );
5963
}
6064

65+
@Test
66+
void testFindWithIllegalOptions(SessionFactoryScope factoryScope) {
67+
factoryScope.inTransaction( (session) -> {
68+
assertThrows( IllegalArgumentException.class, () ->session.find( "artist", 1, SessionChecking.ENABLED ) );
69+
assertThrows( IllegalArgumentException.class, () ->session.find( "artist", 1, OrderedReturn.ORDERED ) );
70+
assertThrows( IllegalArgumentException.class, () ->session.find( "artist", 1, IncludeRemovals.INCLUDE ) );
71+
} );
72+
}
73+
6174
@Test
6275
void testFindWithGraph(SessionFactoryScope factoryScope) {
6376
factoryScope.inTransaction( (session) -> {

0 commit comments

Comments
 (0)