|
13 | 13 | import org.hibernate.engine.spi.PersistenceContext;
|
14 | 14 | import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
15 | 15 | import org.hibernate.query.ResultListTransformer;
|
16 |
| -import org.hibernate.query.spi.QueryOptions; |
| 16 | +import org.hibernate.sql.exec.spi.ExecutionContext; |
17 | 17 | import org.hibernate.sql.results.internal.RowProcessingStateStandardImpl;
|
18 | 18 | import org.hibernate.sql.results.jdbc.internal.JdbcValuesSourceProcessingStateStandardImpl;
|
19 | 19 | import org.hibernate.sql.results.jdbc.spi.JdbcValues;
|
@@ -46,23 +46,13 @@ public class ListResultsConsumer<R> implements ResultsConsumer<List<R>, R> {
|
46 | 46 |
|
47 | 47 | @SuppressWarnings("unchecked")
|
48 | 48 | public static <R> ListResultsConsumer<R> instance(UniqueSemantic uniqueSemantic) {
|
49 |
| - switch ( uniqueSemantic ) { |
50 |
| - case ASSERT: { |
51 |
| - return (ListResultsConsumer<R>) ERROR_DUP_CONSUMER; |
52 |
| - } |
53 |
| - case FILTER: { |
54 |
| - return (ListResultsConsumer<R>) DE_DUP_CONSUMER; |
55 |
| - } |
56 |
| - case NEVER: { |
57 |
| - return (ListResultsConsumer<R>) NEVER_DE_DUP_CONSUMER; |
58 |
| - } |
59 |
| - case ALLOW: { |
60 |
| - return (ListResultsConsumer<R>) ALLOW_DE_DUP_CONSUMER; |
61 |
| - } |
62 |
| - default: { |
63 |
| - return (ListResultsConsumer<R>) IGNORE_DUP_CONSUMER; |
64 |
| - } |
65 |
| - } |
| 49 | + return (ListResultsConsumer<R>) switch ( uniqueSemantic ) { |
| 50 | + case ASSERT -> ERROR_DUP_CONSUMER; |
| 51 | + case FILTER -> DE_DUP_CONSUMER; |
| 52 | + case NEVER -> NEVER_DE_DUP_CONSUMER; |
| 53 | + case ALLOW -> ALLOW_DE_DUP_CONSUMER; |
| 54 | + default -> IGNORE_DUP_CONSUMER; |
| 55 | + }; |
66 | 56 | }
|
67 | 57 |
|
68 | 58 | /**
|
@@ -157,59 +147,26 @@ public List<R> consume(
|
157 | 147 | JdbcValuesSourceProcessingStateStandardImpl jdbcValuesSourceProcessingState,
|
158 | 148 | RowProcessingStateStandardImpl rowProcessingState,
|
159 | 149 | RowReader<R> rowReader) {
|
160 |
| - final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); |
161 |
| - final TypeConfiguration typeConfiguration = session.getTypeConfiguration(); |
162 |
| - final QueryOptions queryOptions = rowProcessingState.getQueryOptions(); |
163 |
| - |
164 | 150 | rowReader.startLoading( rowProcessingState );
|
165 | 151 |
|
166 | 152 | RuntimeException ex = null;
|
| 153 | + final PersistenceContext persistenceContext = session.getPersistenceContextInternal(); |
167 | 154 | persistenceContext.beforeLoad();
|
168 | 155 | persistenceContext.getLoadContexts().register( jdbcValuesSourceProcessingState );
|
169 | 156 | try {
|
170 | 157 | final JavaType<R> domainResultJavaType = resolveDomainResultJavaType(
|
171 | 158 | rowReader.getDomainResultResultJavaType(),
|
172 | 159 | rowReader.getResultJavaTypes(),
|
173 |
| - typeConfiguration |
| 160 | + session.getTypeConfiguration() |
174 | 161 | );
|
175 | 162 |
|
176 | 163 | final boolean isEntityResultType = domainResultJavaType instanceof EntityJavaType;
|
177 | 164 | final int initialCollectionSize = Math.min( jdbcValues.getResultCountEstimate(), INITIAL_COLLECTION_SIZE_LIMIT );
|
178 |
| - |
179 |
| - final Results<R> results; |
180 |
| - if ( isEntityResultType |
181 |
| - && ( uniqueSemantic == UniqueSemantic.ALLOW |
182 |
| - || uniqueSemantic == UniqueSemantic.FILTER ) ) { |
183 |
| - results = new EntityResult<>( domainResultJavaType, initialCollectionSize ); |
184 |
| - } |
185 |
| - else { |
186 |
| - results = new Results<>( domainResultJavaType, initialCollectionSize ); |
187 |
| - } |
188 |
| - |
189 |
| - final int readRows; |
190 |
| - if ( uniqueSemantic == UniqueSemantic.FILTER |
191 |
| - || uniqueSemantic == UniqueSemantic.ASSERT && rowReader.hasCollectionInitializers() |
192 |
| - || uniqueSemantic == UniqueSemantic.ALLOW && isEntityResultType ) { |
193 |
| - readRows = readUnique( rowProcessingState, rowReader, results ); |
194 |
| - } |
195 |
| - else if ( uniqueSemantic == UniqueSemantic.ASSERT ) { |
196 |
| - readRows = readUniqueAssert( rowProcessingState, rowReader, results ); |
197 |
| - } |
198 |
| - else { |
199 |
| - readRows = read( rowProcessingState, rowReader, results ); |
200 |
| - } |
201 |
| - |
| 165 | + final Results<R> results = createResults( isEntityResultType, domainResultJavaType, initialCollectionSize ); |
| 166 | + final int readRows = readRows( rowProcessingState, rowReader, isEntityResultType, results ); |
202 | 167 | rowReader.finishUp( rowProcessingState );
|
203 | 168 | jdbcValuesSourceProcessingState.finishUp( readRows > 1 );
|
204 |
| - |
205 |
| - //noinspection unchecked |
206 |
| - final ResultListTransformer<R> resultListTransformer = |
207 |
| - (ResultListTransformer<R>) queryOptions.getResultListTransformer(); |
208 |
| - if ( resultListTransformer != null ) { |
209 |
| - return resultListTransformer.transformList( results.getResults() ); |
210 |
| - } |
211 |
| - |
212 |
| - return results.getResults(); |
| 169 | + return transformList( rowProcessingState, results ); |
213 | 170 | }
|
214 | 171 | catch (RuntimeException e) {
|
215 | 172 | ex = e;
|
@@ -238,6 +195,47 @@ else if ( uniqueSemantic == UniqueSemantic.ASSERT ) {
|
238 | 195 | throw new IllegalStateException( "Should not reach this" );
|
239 | 196 | }
|
240 | 197 |
|
| 198 | + private static <R> List<R> transformList(ExecutionContext executionContext, Results<R> results) { |
| 199 | + final ResultListTransformer<R> transformer = getResultListTransformer( executionContext ); |
| 200 | + return transformer == null ? results.getResults() : transformer.transformList( results.getResults() ); |
| 201 | + } |
| 202 | + |
| 203 | + @SuppressWarnings("unchecked") |
| 204 | + private static <R> ResultListTransformer<R> getResultListTransformer(ExecutionContext executionContext) { |
| 205 | + return (ResultListTransformer<R>) executionContext.getQueryOptions().getResultListTransformer(); |
| 206 | + } |
| 207 | + |
| 208 | + private Results<R> createResults( |
| 209 | + boolean isEntityResultType, |
| 210 | + JavaType<R> domainResultJavaType, |
| 211 | + int initialCollectionSize) { |
| 212 | + if ( isEntityResultType |
| 213 | + && ( uniqueSemantic == UniqueSemantic.ALLOW || uniqueSemantic == UniqueSemantic.FILTER ) ) { |
| 214 | + return new EntityResult<>( domainResultJavaType, initialCollectionSize ); |
| 215 | + } |
| 216 | + else { |
| 217 | + return new Results<>( domainResultJavaType, initialCollectionSize ); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + private int readRows( |
| 222 | + RowProcessingStateStandardImpl rowProcessingState, |
| 223 | + RowReader<R> rowReader, |
| 224 | + boolean isEntityResultType, |
| 225 | + Results<R> results) { |
| 226 | + if ( uniqueSemantic == UniqueSemantic.FILTER |
| 227 | + || uniqueSemantic == UniqueSemantic.ASSERT && rowReader.hasCollectionInitializers() |
| 228 | + || uniqueSemantic == UniqueSemantic.ALLOW && isEntityResultType ) { |
| 229 | + return readUnique( rowProcessingState, rowReader, results ); |
| 230 | + } |
| 231 | + else if ( uniqueSemantic == UniqueSemantic.ASSERT ) { |
| 232 | + return readUniqueAssert( rowProcessingState, rowReader, results ); |
| 233 | + } |
| 234 | + else { |
| 235 | + return read( rowProcessingState, rowReader, results ); |
| 236 | + } |
| 237 | + } |
| 238 | + |
241 | 239 | private static <R> int read(
|
242 | 240 | RowProcessingStateStandardImpl rowProcessingState,
|
243 | 241 | RowReader<R> rowReader,
|
|
0 commit comments