|
6 | 6 |
|
7 | 7 | import java.io.Serializable; |
8 | 8 | import java.time.Instant; |
9 | | -import java.util.Calendar; |
10 | | -import java.util.Collection; |
11 | | -import java.util.Collections; |
12 | | -import java.util.Date; |
13 | | -import java.util.HashSet; |
14 | | -import java.util.List; |
15 | | -import java.util.Locale; |
16 | | -import java.util.Map; |
17 | | -import java.util.Set; |
| 9 | +import java.util.*; |
18 | 10 | import java.util.function.Consumer; |
19 | 11 | import java.util.function.Supplier; |
20 | 12 |
|
@@ -134,6 +126,8 @@ public class NativeQueryImpl<R> |
134 | 126 | private Set<String> querySpaces; |
135 | 127 | private Callback callback; |
136 | 128 |
|
| 129 | + private Class resultEntityClass; |
| 130 | + |
137 | 131 | /** |
138 | 132 | * Constructs a NativeQueryImpl given a sql query defined in the mappings. |
139 | 133 | */ |
@@ -218,12 +212,19 @@ public NativeQueryImpl( |
218 | 212 | session |
219 | 213 | ); |
220 | 214 |
|
| 215 | + if ( resultJavaType != null && getSessionFactory().getMappingMetamodel().isEntityClass( resultJavaType ) ) { |
| 216 | + resultEntityClass = resultJavaType; |
| 217 | + } |
| 218 | + |
221 | 219 | if ( resultJavaType == Tuple.class ) { |
222 | 220 | setTupleTransformer( new NativeQueryTupleTransformer() ); |
223 | 221 | } |
224 | 222 | else if ( resultJavaType != null && !resultJavaType.isArray() ) { |
225 | 223 | switch ( resultSetMapping.getNumberOfResultBuilders() ) { |
226 | 224 | case 0: { |
| 225 | + if (resultEntityClass != null) { |
| 226 | + break; |
| 227 | + } |
227 | 228 | throw new IllegalArgumentException( "Named query exists, but did not specify a resultClass" ); |
228 | 229 | } |
229 | 230 | case 1: { |
@@ -354,6 +355,10 @@ private static ResultSetMapping buildResultSetMapping( |
354 | 355 | return ResultSetMapping.resolveResultSetMapping( registeredName, isDynamic, session.getFactory() ); |
355 | 356 | } |
356 | 357 |
|
| 358 | + public void setResultEntityClass(Class<?> resultEntityClass) { |
| 359 | + this.resultEntityClass = resultEntityClass; |
| 360 | + } |
| 361 | + |
357 | 362 | public List<ParameterOccurrence> getParameterOccurrences() { |
358 | 363 | return parameterOccurrences; |
359 | 364 | } |
@@ -459,7 +464,7 @@ public QueryParameterBindings getParameterBindings() { |
459 | 464 | public NamedNativeQueryMemento<?> toMemento(String name) { |
460 | 465 | return new NamedNativeQueryMementoImpl<>( |
461 | 466 | name, |
462 | | - extractResultClass( resultSetMapping ), |
| 467 | + resultEntityClass !=null ? resultEntityClass : extractResultClass( resultSetMapping ), |
463 | 468 | sqlString, |
464 | 469 | originalSqlString, |
465 | 470 | resultSetMapping.getMappingIdentifier(), |
@@ -644,6 +649,18 @@ public KeyedResultList<R> getKeyedResultList(KeyedPage<R> page) { |
644 | 649 | } |
645 | 650 |
|
646 | 651 | protected SelectQueryPlan<R> resolveSelectQueryPlan() { |
| 652 | + if ( resultEntityClass != null ) { |
| 653 | + boolean exists = false; |
| 654 | + for ( ResultBuilder existing : resultSetMapping.getResultBuilders() ) { |
| 655 | + if ( Objects.equals( resultEntityClass, existing.getJavaType() ) ) { |
| 656 | + exists = true; |
| 657 | + break; |
| 658 | + } |
| 659 | + } |
| 660 | + if ( !exists ) { |
| 661 | + addEntity( (Class<R>) resultEntityClass, LockMode.READ ); |
| 662 | + } |
| 663 | + } |
647 | 664 | if ( isCacheableQuery() ) { |
648 | 665 | final QueryInterpretationCache.Key cacheKey = generateSelectInterpretationsKey( resultSetMapping ); |
649 | 666 | return getSession().getFactory().getQueryEngine().getInterpretationCache() |
|
0 commit comments