-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
AbstractProducedQuery.java
1187 lines (1053 loc) · 33.6 KB
/
AbstractProducedQuery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.query.internal;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.CacheRetrieveMode;
import javax.persistence.CacheStoreMode;
import javax.persistence.FlushModeType;
import javax.persistence.LockModeType;
import javax.persistence.Parameter;
import javax.persistence.TemporalType;
import org.hibernate.CacheMode;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.NonUniqueResultException;
import org.hibernate.PropertyNotFoundException;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.TypeMismatchException;
import org.hibernate.engine.query.spi.EntityGraphQueryHint;
import org.hibernate.engine.query.spi.HQLQueryPlan;
import org.hibernate.engine.spi.QueryParameters;
import org.hibernate.engine.spi.RowSelection;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.TypedValue;
import org.hibernate.hql.internal.QueryExecutionRequestException;
import org.hibernate.internal.EntityManagerMessageLogger;
import org.hibernate.internal.HEMLogging;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.QueryHints;
import org.hibernate.jpa.graph.internal.EntityGraphImpl;
import org.hibernate.jpa.internal.util.CacheModeHelper;
import org.hibernate.jpa.internal.util.ConfigurationHelper;
import org.hibernate.jpa.internal.util.FlushModeTypeHelper;
import org.hibernate.jpa.internal.util.LockModeTypeHelper;
import org.hibernate.property.access.spi.BuiltInPropertyAccessStrategies;
import org.hibernate.property.access.spi.Getter;
import org.hibernate.property.access.spi.PropertyAccess;
import org.hibernate.query.ParameterMetadata;
import org.hibernate.query.QueryParameter;
import org.hibernate.query.spi.QueryImplementor;
import org.hibernate.query.spi.QueryParameterBinding;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.type.Type;
import static org.hibernate.jpa.QueryHints.HINT_CACHEABLE;
import static org.hibernate.jpa.QueryHints.HINT_CACHE_MODE;
import static org.hibernate.jpa.QueryHints.HINT_CACHE_REGION;
import static org.hibernate.jpa.QueryHints.HINT_COMMENT;
import static org.hibernate.jpa.QueryHints.HINT_FETCHGRAPH;
import static org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE;
import static org.hibernate.jpa.QueryHints.HINT_FLUSH_MODE;
import static org.hibernate.jpa.QueryHints.HINT_LOADGRAPH;
import static org.hibernate.jpa.QueryHints.HINT_NATIVE_LOCKMODE;
import static org.hibernate.jpa.QueryHints.HINT_READONLY;
import static org.hibernate.jpa.QueryHints.HINT_TIMEOUT;
import static org.hibernate.jpa.QueryHints.SPEC_HINT_TIMEOUT;
/**
* @author Steve Ebersole
*/
public abstract class AbstractProducedQuery<R> implements QueryImplementor<R> {
private static final EntityManagerMessageLogger log = HEMLogging.messageLogger( AbstractProducedQuery.class );
private final SharedSessionContractImplementor producer;
private final ParameterMetadata parameterMetadata;
private final QueryParameterBindingsImpl queryParameterBindings;
private FlushMode flushMode;
private CacheMode cacheMode;
private Integer timeout;
private boolean cacheable;
private String cacheRegion;
private Boolean readOnly;
private LockOptions lockOptions = new LockOptions();
private Integer fetchSize;
private String comment;
private final List<String> dbHints = new ArrayList<>();
private Map<String, Object> hints;
private ResultTransformer resultTransformer;
private RowSelection selection = new RowSelection();
private HQLQueryPlan entityGraphHintedQueryPlan;
private Object optionalObject;
private Serializable optionalId;
private String optionalEntityName;
public AbstractProducedQuery(
SharedSessionContractImplementor producer,
ParameterMetadata parameterMetadata) {
this.producer = producer;
this.parameterMetadata = parameterMetadata;
this.queryParameterBindings = QueryParameterBindingsImpl.from( parameterMetadata, producer.getFactory() );
}
@Override
public SharedSessionContractImplementor getProducer() {
return producer;
}
@Override
public FlushMode getHibernateFlushMode() {
return flushMode;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setHibernateFlushMode(FlushMode flushMode) {
this.flushMode = flushMode;
return this;
}
@Override
public QueryImplementor setFlushMode(FlushMode flushMode) {
return setHibernateFlushMode( flushMode );
}
@Override
public FlushModeType getFlushMode() {
return ( flushMode == null ?
getProducer().getFlushMode() :
FlushModeTypeHelper.getFlushModeType( flushMode )
);
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setFlushMode(FlushModeType flushModeType) {
setHibernateFlushMode( FlushModeTypeHelper.getFlushMode( flushModeType ) );
return this;
}
@Override
public CacheMode getCacheMode() {
return cacheMode;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setCacheMode(CacheMode cacheMode) {
this.cacheMode = cacheMode;
return this;
}
@Override
public boolean isCacheable() {
return cacheable;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setCacheable(boolean cacheable) {
this.cacheable = cacheable;
return this;
}
@Override
public String getCacheRegion() {
return cacheRegion;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setCacheRegion(String cacheRegion) {
this.cacheRegion = cacheRegion;
return this;
}
@Override
public Integer getTimeout() {
return timeout;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setTimeout(int timeout) {
this.timeout = timeout;
return this;
}
@Override
public Integer getFetchSize() {
return fetchSize;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setFetchSize(int fetchSize) {
this.fetchSize = fetchSize;
return this;
}
@Override
public boolean isReadOnly() {
return ( readOnly == null ?
producer.getPersistenceContext().isDefaultReadOnly() :
readOnly
);
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setReadOnly(boolean readOnly) {
this.readOnly = readOnly;
return this;
}
@Override
public LockOptions getLockOptions() {
return lockOptions;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setLockOptions(LockOptions lockOptions) {
this.lockOptions.setLockMode( lockOptions.getLockMode() );
this.lockOptions.setScope( lockOptions.getScope() );
this.lockOptions.setTimeOut( lockOptions.getTimeOut() );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setLockMode(String alias, LockMode lockMode) {
lockOptions.setAliasSpecificLockMode( alias, lockMode );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setLockMode(LockModeType lockModeType) {
if ( !LockModeType.NONE.equals( lockModeType ) ) {
if ( !isSelect() ) {
throw new IllegalStateException( "Illegal attempt to set lock mode on a non-SELECT query" );
}
}
lockOptions.setLockMode( LockModeTypeHelper.getLockMode( lockModeType ) );
return this;
}
@Override
public String getComment() {
return comment;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setComment(String comment) {
this.comment = comment;
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor addQueryHint(String hint) {
this.dbHints.add( hint );
return this;
}
@Override
public ParameterMetadata getParameterMetadata() {
return parameterMetadata;
}
@Override
public String[] getNamedParameters() {
return ArrayHelper.toStringArray( getParameterMetadata().getNamedParameterNames() );
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(QueryParameter parameter, Object value) {
queryParameterBindings.getBinding( parameter ).setBindValue( value );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(Parameter parameter, Object value) {
queryParameterBindings.getBinding( (QueryParameter) parameter ).setBindValue( value );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(String name, Object value) {
queryParameterBindings.getBinding( name ).setBindValue( value );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(int position, Object value) {
queryParameterBindings.getBinding( position ).setBindValue( value );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(QueryParameter parameter, Object value, Type type) {
queryParameterBindings.getBinding( parameter ).setBindValue( value, type );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(String name, Object value, Type type) {
queryParameterBindings.getBinding( name ).setBindValue( value, type );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(int position, Object value, Type type) {
queryParameterBindings.getBinding( position ).setBindValue( value, type );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(QueryParameter parameter, Object value, TemporalType temporalType) {
queryParameterBindings.getBinding( parameter ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(String name, Object value, TemporalType temporalType) {
queryParameterBindings.getBinding( name ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(int position, Object value, TemporalType temporalType) {
queryParameterBindings.getBinding( position ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameterList(QueryParameter parameter, Collection values) {
queryParameterBindings.getQueryParameterListBinding( parameter ).setBindValues( values );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameterList(String name, Collection values) {
queryParameterBindings.getQueryParameterListBinding( name ).setBindValues( values );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameterList(String name, Collection values, Type type) {
queryParameterBindings.getQueryParameterListBinding( name ).setBindValues( values, type );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameterList(String name, Object[] values, Type type) {
queryParameterBindings.getQueryParameterListBinding( name ).setBindValues( Arrays.asList( values ), type );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameterList(String name, Object[] values) {
queryParameterBindings.getQueryParameterListBinding( name ).setBindValues( Arrays.asList( values ) );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(Parameter param, Calendar value, TemporalType temporalType) {
queryParameterBindings.getBinding( (QueryParameter) param ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(Parameter param, Date value, TemporalType temporalType) {
queryParameterBindings.getBinding( (QueryParameter) param ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(String name, Calendar value, TemporalType temporalType) {
queryParameterBindings.getBinding( name ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(String name, Date value, TemporalType temporalType) {
queryParameterBindings.getBinding( name ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(int position, Calendar value, TemporalType temporalType) {
queryParameterBindings.getBinding( position ).setBindValue( value, temporalType );
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setParameter(int position, Date value, TemporalType temporalType) {
queryParameterBindings.getBinding( position ).setBindValue( value, temporalType );
return this;
}
@Override
public Set<Parameter<?>> getParameters() {
return parameterMetadata.collectAllParametersJpa();
}
@Override
public Parameter<?> getParameter(String name) {
return parameterMetadata.getQueryParameter( name );
}
@Override
@SuppressWarnings("unchecked")
public <T> Parameter<T> getParameter(String name, Class<T> type) {
final QueryParameter parameter = parameterMetadata.getQueryParameter( name );
if ( !parameter.getParameterType().isAssignableFrom( type ) ) {
throw new IllegalArgumentException(
"The type [" + parameter.getParameterType().getName() +
"] associated with the parameter corresponding to name [" + name +
"] is not assignable to requested Java type [" + type.getName() + "]"
);
}
return parameter;
}
@Override
public Parameter<?> getParameter(int position) {
return parameterMetadata.getQueryParameter( position );
}
@Override
@SuppressWarnings("unchecked")
public <T> Parameter<T> getParameter(int position, Class<T> type) {
final QueryParameter parameter = parameterMetadata.getQueryParameter( position );
if ( !parameter.getParameterType().isAssignableFrom( type ) ) {
throw new IllegalArgumentException(
"The type [" + parameter.getParameterType().getName() +
"] associated with the parameter corresponding to position [" + position +
"] is not assignable to requested Java type [" + type.getName() + "]"
);
}
return parameter;
}
@Override
public boolean isBound(Parameter<?> parameter) {
return queryParameterBindings.isBound( (QueryParameter) parameter );
}
@Override
@SuppressWarnings("unchecked")
public <T> T getParameterValue(Parameter<T> parameter) {
return (T) queryParameterBindings.getBinding( (QueryParameter) parameter ).getBindValue();
}
@Override
public Object getParameterValue(String name) {
return queryParameterBindings.getBinding( name ).getBindValue();
}
@Override
public Object getParameterValue(int position) {
return queryParameterBindings.getBinding( position ).getBindValue();
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setProperties(Object bean) {
Class clazz = bean.getClass();
String[] params = getNamedParameters();
for ( String namedParam : params ) {
try {
final PropertyAccess propertyAccess = BuiltInPropertyAccessStrategies.BASIC.getStrategy().buildPropertyAccess(
clazz,
namedParam
);
final Getter getter = propertyAccess.getGetter();
final Class retType = getter.getReturnType();
final Object object = getter.get( bean );
if ( Collection.class.isAssignableFrom( retType ) ) {
setParameterList( namedParam, (Collection) object );
}
else if ( retType.isArray() ) {
setParameterList( namedParam, (Object[]) object );
}
else {
Type type = determineType( namedParam, retType );
setParameter( namedParam, object, type );
}
}
catch (PropertyNotFoundException pnfe) {
// ignore
}
}
return this;
}
protected Type determineType(String namedParam, Class retType) {
Type type = queryParameterBindings.getBinding( namedParam ).getBindType();
if ( type == null ) {
type = parameterMetadata.getQueryParameter( namedParam ).getType();
}
if ( type == null ) {
type = getProducer().getFactory().resolveParameterBindType( retType );
}
return type;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setProperties(Map map) {
final String[] namedParameterNames = getNamedParameters();
for ( String paramName : namedParameterNames ) {
final Object object = map.get( paramName );
if ( object == null ) {
setParameter( paramName, null, determineType( paramName, null ) );
}
else {
Class retType = object.getClass();
if ( Collection.class.isAssignableFrom( retType ) ) {
setParameterList( paramName, (Collection) object );
}
else if ( retType.isArray() ) {
setParameterList( paramName, (Object[]) object );
}
else {
setParameter( paramName, object, determineType( paramName, retType ) );
}
}
}
return this;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setResultTransformer(ResultTransformer transformer) {
this.resultTransformer = transformer;
return this;
}
@Override
public int getMaxResults() {
return selection.getMaxRows();
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setMaxResults(int maxResult) {
if ( maxResult <= 0 ) {
// treat zero and negatives specially as meaning no limit...
selection.setMaxRows( null );
}
else {
selection.setMaxRows( maxResult );
}
return this;
}
@Override
public int getFirstResult() {
return selection.getFirstRow();
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setFirstResult(int startPosition) {
selection.setFirstRow( startPosition );
return this;
}
@SuppressWarnings( {"UnusedDeclaration"})
public Set<String> getSupportedHints() {
return QueryHints.getDefinedHints();
}
@Override
public Map<String, Object> getHints() {
getProducer().checkOpen( false ); // technically should rollback
return hints;
}
@Override
@SuppressWarnings("unchecked")
public QueryImplementor setHint(String hintName, Object value) {
getProducer().checkOpen( true );
boolean applied = false;
try {
if ( HINT_TIMEOUT.equals( hintName ) ) {
applied = applyTimeoutHint( ConfigurationHelper.getInteger( value ) );
}
else if ( SPEC_HINT_TIMEOUT.equals( hintName ) ) {
// convert milliseconds to seconds
int timeout = (int)Math.round( ConfigurationHelper.getInteger( value ).doubleValue() / 1000.0 );
applied = applyTimeoutHint( timeout );
}
else if ( AvailableSettings.LOCK_TIMEOUT.equals( hintName ) ) {
applied = applyLockTimeoutHint( ConfigurationHelper.getInteger( value ) );
}
else if ( HINT_COMMENT.equals( hintName ) ) {
applied = applyCommentHint( (String) value );
}
else if ( HINT_FETCH_SIZE.equals( hintName ) ) {
applied = applyFetchSizeHint( ConfigurationHelper.getInteger( value ) );
}
else if ( HINT_CACHEABLE.equals( hintName ) ) {
applied = applyCacheableHint( ConfigurationHelper.getBoolean( value ) );
}
else if ( HINT_CACHE_REGION.equals( hintName ) ) {
applied = applyCacheRegionHint( (String) value );
}
else if ( HINT_READONLY.equals( hintName ) ) {
applied = applyReadOnlyHint( ConfigurationHelper.getBoolean( value ) );
}
else if ( HINT_CACHE_MODE.equals( hintName ) ) {
applied = applyCacheModeHint( ConfigurationHelper.getCacheMode( value ) );
}
else if ( HINT_FLUSH_MODE.equals( hintName ) ) {
applied = applyFlushModeHint( ConfigurationHelper.getFlushMode( value ) );
}
else if ( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE.equals( hintName ) ) {
final CacheRetrieveMode retrieveMode = value != null ? CacheRetrieveMode.valueOf( value.toString() ) : null;
final CacheStoreMode storeMode = getHint( AvailableSettings.SHARED_CACHE_STORE_MODE, CacheStoreMode.class );
applied = applyCacheModeHint( CacheModeHelper.interpretCacheMode( storeMode, retrieveMode ) );
}
else if ( AvailableSettings.SHARED_CACHE_STORE_MODE.equals( hintName ) ) {
final CacheStoreMode storeMode = value != null ? CacheStoreMode.valueOf( value.toString() ) : null;
final CacheRetrieveMode retrieveMode = getHint( AvailableSettings.SHARED_CACHE_RETRIEVE_MODE, CacheRetrieveMode.class );
applied = applyCacheModeHint( CacheModeHelper.interpretCacheMode( storeMode, retrieveMode ) );
}
else if ( QueryHints.HINT_NATIVE_LOCKMODE.equals( hintName ) ) {
if ( !isNativeQuery() ) {
throw new IllegalStateException(
"Illegal attempt to set lock mode on non-native query via hint; use Query#setLockMode instead"
);
}
if ( LockMode.class.isInstance( value ) ) {
applyHibernateLockModeHint( (LockMode) value );
}
else if ( LockModeType.class.isInstance( value ) ) {
applyLockModeTypeHint( (LockModeType) value );
}
else {
throw new IllegalArgumentException(
String.format(
"Native lock-mode hint [%s] must specify %s or %s. Encountered type : %s",
HINT_NATIVE_LOCKMODE,
LockMode.class.getName(),
LockModeType.class.getName(),
value.getClass().getName()
)
);
}
applied = true;
}
else if ( hintName.startsWith( AvailableSettings.ALIAS_SPECIFIC_LOCK_MODE ) ) {
if ( canApplyAliasSpecificLockModeHints() ) {
// extract the alias
final String alias = hintName.substring( AvailableSettings.ALIAS_SPECIFIC_LOCK_MODE.length() + 1 );
// determine the LockMode
try {
final LockMode lockMode = LockModeTypeHelper.interpretLockMode( value );
applyAliasSpecificLockModeHint( alias, lockMode );
}
catch ( Exception e ) {
log.unableToDetermineLockModeValue( hintName, value );
applied = false;
}
}
else {
applied = false;
}
}
else if ( HINT_FETCHGRAPH.equals( hintName ) || HINT_LOADGRAPH.equals( hintName ) ) {
if (value instanceof EntityGraphImpl ) {
applyEntityGraphQueryHint( new EntityGraphQueryHint( (EntityGraphImpl) value ) );
}
else {
log.warnf( "The %s hint was set, but the value was not an EntityGraph!", hintName );
}
applied = true;
}
else {
log.ignoringUnrecognizedQueryHint( hintName );
}
}
catch ( ClassCastException e ) {
throw new IllegalArgumentException( "Value for hint" );
}
if ( applied ) {
if ( hints == null ) {
hints = new HashMap<>();
}
hints.put( hintName, value );
}
else {
log.debugf( "Skipping unsupported query hint [%s]", hintName );
}
return this;
}
private <T extends Enum<T>> T getHint(String key, Class<T> hintClass) {
Object hint = hints != null ? hints.get( key ) : null;
// todo : we need this
// if ( hint == null ) {
// hint = getProducer().getProperties().get( key );
// }
return hint != null ? Enum.valueOf( hintClass, hint.toString() ) : null;
}
/**
* Apply the query timeout hint.
*
* @param timeout The timeout (in seconds!) specified as a hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyTimeoutHint(int timeout) {
setTimeout( timeout );
return true;
}
/**
* Apply the lock timeout (in seconds!) hint
*
* @param timeout The timeout (in seconds!) specified as a hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyLockTimeoutHint(int timeout) {
getLockOptions().setTimeOut( timeout );
return true;
}
/**
* Apply the comment hint.
*
* @param comment The comment specified as a hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyCommentHint(String comment) {
setComment( comment );
return true;
}
/**
* Apply the fetch size hint
*
* @param fetchSize The fetch size specified as a hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyFetchSizeHint(int fetchSize) {
setFetchSize( fetchSize );
return true;
}
/**
* Apply the cacheable (true/false) hint.
*
* @param isCacheable The value specified as hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyCacheableHint(boolean isCacheable) {
setCacheable( isCacheable );
return true;
}
/**
* Apply the cache region hint
*
* @param regionName The name of the cache region specified as a hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyCacheRegionHint(String regionName) {
setCacheRegion( regionName );
return true;
}
/**
* Apply the read-only (true/false) hint.
*
* @param isReadOnly The value specified as hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyReadOnlyHint(boolean isReadOnly) {
setReadOnly( isReadOnly );
return true;
}
/**
* Apply the CacheMode hint.
*
* @param cacheMode The CacheMode value specified as a hint.
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyCacheModeHint(CacheMode cacheMode) {
setCacheMode( cacheMode );
return true;
}
/**
* Apply the FlushMode hint.
*
* @param flushMode The FlushMode value specified as hint
*
* @return {@code true} if the hint was "applied"
*/
protected boolean applyFlushModeHint(FlushMode flushMode) {
setFlushMode( flushMode );
return true;
}
/**
* Can alias-specific lock modes be applied?
*
* @return {@code true} indicates they can be applied, {@code false} otherwise.
*/
protected boolean canApplyAliasSpecificLockModeHints() {
// only procedure/function calls cannot i believe
return true;
}
protected boolean applyLockModeTypeHint(LockModeType lockModeType) {
getLockOptions().setLockMode( LockModeTypeHelper.getLockMode( lockModeType ) );
return true;
}
protected boolean applyHibernateLockModeHint(LockMode lockMode) {
getLockOptions().setLockMode( lockMode );
return true;
}
/**
* Apply the alias specific lock modes. Assumes {@link #canApplyAliasSpecificLockModeHints()} has already been
* called and returned {@code true}.
*
* @param alias The alias to apply the 'lockMode' to.
* @param lockMode The LockMode to apply.
*/
protected void applyAliasSpecificLockModeHint(String alias, LockMode lockMode) {
getLockOptions().setAliasSpecificLockMode( alias, lockMode );
}
/**
* Used from HEM code as a (hopefully temporary) means to apply a custom query plan
* in regards to a JPA entity graph.
*
* @param hint The entity graph hint object
*/
public void applyEntityGraphQueryHint(EntityGraphQueryHint hint) {
queryParameterBindings.verifyParametersBound( false );
// todo : ideally we'd update the instance state related to queryString but that is final atm
final String expandedQuery = queryParameterBindings.expandListValuedParameters( getQueryString(), getProducer() );
this.entityGraphHintedQueryPlan = new HQLQueryPlan(
expandedQuery,
false,
getProducer().getLoadQueryInfluencers().getEnabledFilters(),
getProducer().getFactory(),
hint
);
}
/**
* Is the query represented here a native (SQL) query?
*
* @return {@code true} if it is a native query; {@code false} otherwise
*/
protected abstract boolean isNativeQuery();
@Override
public LockModeType getLockMode() {
return LockModeTypeHelper.getLockModeType( lockOptions.getLockMode() );
}
@Override
@SuppressWarnings("unchecked")
public <T> T unwrap(Class<T> cls) {
if ( cls.isInstance( getProducer() ) ) {
return (T) getProducer();
}
if ( cls.isInstance( getParameterMetadata() ) ) {
return (T) getParameterMetadata();
}
if ( cls.isInstance( queryParameterBindings ) ) {
return (T) queryParameterBindings;
}
if ( cls.isInstance( this ) ) {
return (T) this;
}
throw new IllegalArgumentException( "Could not unwrap this [" + toString() + "] as requested Java type [" + cls.getName() + "]" );
}
public QueryParameters getQueryParameters() {
QueryParameters queryParameters = new QueryParameters(
getPositionalParameterTypes(),
getPositionalParameterValues(),
getNamedParameterMap(),
getLockOptions(),
selection,
true,
isReadOnly(),
cacheable,
cacheRegion,
comment,
dbHints,
null,
optionalObject,
optionalEntityName,
optionalId,
resultTransformer
);
queryParameters.setQueryPlan( entityGraphHintedQueryPlan );
return queryParameters;
}
@SuppressWarnings("deprecation")
protected Type[] getPositionalParameterTypes() {
return queryParameterBindings.collectPositionalBindTypes();
}
@SuppressWarnings("deprecation")
protected Object[] getPositionalParameterValues() {
return queryParameterBindings.collectPositionalBindValues();
}
@SuppressWarnings("deprecation")
protected Map<String, TypedValue> getNamedParameterMap() {
return queryParameterBindings.collectNamedParameterBindings();
}
private FlushMode sessionFlushMode;
private CacheMode sessionCacheMode;
protected void beforeQuery() {
queryParameterBindings.verifyParametersBound( isCallable() );
assert sessionFlushMode == null;
assert sessionCacheMode == null;
if ( flushMode != null ) {
sessionFlushMode = getProducer().getHibernateFlushMode();
getProducer().setHibernateFlushMode( flushMode );
}
if ( cacheMode != null ) {
sessionCacheMode = getProducer().getCacheMode();
getProducer().setCacheMode( cacheMode );
}
}