44 */
55package org .hibernate .loader .ast .internal ;
66
7- import java .util .List ;
8-
97import org .hibernate .LockOptions ;
108import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
119import org .hibernate .engine .jdbc .spi .JdbcServices ;
1210import org .hibernate .engine .spi .SessionFactoryImplementor ;
1311import org .hibernate .engine .spi .SharedSessionContractImplementor ;
12+ import org .hibernate .engine .spi .SubselectFetch ;
1413import org .hibernate .loader .ast .spi .Loadable ;
1514import org .hibernate .metamodel .mapping .EntityMappingType ;
1615import org .hibernate .metamodel .mapping .ModelPart ;
1716import org .hibernate .query .internal .SimpleQueryOptions ;
18- import org .hibernate .query .spi .QueryOptions ;
19- import org .hibernate .query .spi .QueryOptionsAdapter ;
2017import org .hibernate .sql .ast .SqlAstTranslatorFactory ;
2118import org .hibernate .sql .ast .tree .select .SelectStatement ;
22- import org .hibernate .sql .exec .internal .BaseExecutionContext ;
2319import org .hibernate .sql .exec .internal .CallbackImpl ;
2420import org .hibernate .sql .exec .internal .JdbcParameterBindingsImpl ;
2521import org .hibernate .sql .exec .spi .Callback ;
3026import org .hibernate .sql .results .spi .ListResultsConsumer ;
3127import org .hibernate .sql .results .spi .RowTransformer ;
3228
29+ import java .util .List ;
30+
3331/**
3432 * Describes a plan for loading an entity by identifier.
3533 *
36- * @implNote Made up of (1) a SQL AST for the SQL SELECT and (2) the `ModelPart` used as the restriction
37- *
3834 * @author Steve Ebersole
35+ * @implNote Made up of (1) a SQL AST for the SQL SELECT and (2) the `ModelPart` used as the restriction
3936 */
4037// todo (6.0) : this can generically define a load-by-uk as well.
4138// only the SQL AST and `restrictivePart` vary and they are passed as constructor args
4239public class SingleIdLoadPlan <T > implements SingleEntityLoadPlan {
4340 private final EntityMappingType entityMappingType ;
4441 private final ModelPart restrictivePart ;
4542 private final LockOptions lockOptions ;
43+ private final SelectStatement sqlAst ;
4644 private final JdbcOperationQuerySelect jdbcSelect ;
4745 private final JdbcParametersList jdbcParameters ;
4846
@@ -57,18 +55,14 @@ public SingleIdLoadPlan(
5755 this .restrictivePart = restrictivePart ;
5856 this .lockOptions = lockOptions .makeCopy ();
5957 this .jdbcParameters = jdbcParameters ;
58+ this .sqlAst = sqlAst ;
6059 final JdbcServices jdbcServices = sessionFactory .getJdbcServices ();
6160 final JdbcEnvironment jdbcEnvironment = jdbcServices .getJdbcEnvironment ();
6261 final SqlAstTranslatorFactory sqlAstTranslatorFactory = jdbcEnvironment .getSqlAstTranslatorFactory ();
6362 this .jdbcSelect = sqlAstTranslatorFactory .buildSelectTranslator ( sessionFactory , sqlAst )
6463 .translate (
6564 null ,
66- new QueryOptionsAdapter () {
67- @ Override
68- public LockOptions getLockOptions () {
69- return lockOptions ;
70- }
71- }
65+ new SimpleQueryOptions ( lockOptions , null )
7266 );
7367 }
7468
@@ -137,23 +131,32 @@ public T load(
137131 );
138132 }
139133 assert offset == jdbcParameters .size ();
140- final QueryOptions queryOptions = new SimpleQueryOptions ( lockOptions , readOnly );
141134 final Callback callback = new CallbackImpl ();
135+ final SubselectFetch .RegistrationHandler subselectRegistrationHandler = SubselectFetch .createRegistrationHandler (
136+ session .getPersistenceContextInternal ().getBatchFetchQueue (),
137+ sqlAst ,
138+ jdbcParameters ,
139+ jdbcParameterBindings
140+ );
142141
143142 final List <T > list = session .getJdbcServices ().getJdbcSelectExecutor ().list (
144143 jdbcSelect ,
145144 jdbcParameterBindings ,
146145 new SingleIdExecutionContext (
147- session ,
148- entityInstance ,
149146 restrictedValue ,
147+ entityInstance ,
150148 entityMappingType .getRootEntityDescriptor (),
151- queryOptions ,
149+ readOnly ,
150+ lockOptions ,
151+ subselectRegistrationHandler ,
152+ session ,
152153 callback
153154 ),
154155 getRowTransformer (),
155156 null ,
156- singleResultExpected ? ListResultsConsumer .UniqueSemantic .ASSERT : ListResultsConsumer .UniqueSemantic .FILTER ,
157+ singleResultExpected ?
158+ ListResultsConsumer .UniqueSemantic .ASSERT :
159+ ListResultsConsumer .UniqueSemantic .FILTER ,
157160 1
158161 );
159162
@@ -165,52 +168,4 @@ public T load(
165168 callback .invokeAfterLoadActions ( entity , entityMappingType , session );
166169 return entity ;
167170 }
168-
169- private static class SingleIdExecutionContext extends BaseExecutionContext {
170- private final Object entityInstance ;
171- private final Object restrictedValue ;
172- private final EntityMappingType rootEntityDescriptor ;
173- private final QueryOptions queryOptions ;
174- private final Callback callback ;
175-
176- public SingleIdExecutionContext (
177- SharedSessionContractImplementor session ,
178- Object entityInstance ,
179- Object restrictedValue ,
180- EntityMappingType rootEntityDescriptor , QueryOptions queryOptions ,
181- Callback callback ) {
182- super ( session );
183- this .entityInstance = entityInstance ;
184- this .restrictedValue = restrictedValue ;
185- this .rootEntityDescriptor = rootEntityDescriptor ;
186- this .queryOptions = queryOptions ;
187- this .callback = callback ;
188- }
189-
190- @ Override
191- public Object getEntityInstance () {
192- return entityInstance ;
193- }
194-
195- @ Override
196- public Object getEntityId () {
197- return restrictedValue ;
198- }
199-
200- @ Override
201- public EntityMappingType getRootEntityDescriptor () {
202- return rootEntityDescriptor ;
203- }
204-
205- @ Override
206- public QueryOptions getQueryOptions () {
207- return queryOptions ;
208- }
209-
210- @ Override
211- public Callback getCallback () {
212- return callback ;
213- }
214-
215- }
216171}
0 commit comments