117
117
import org .hibernate .internal .FilterHelper ;
118
118
import org .hibernate .internal .util .IndexedConsumer ;
119
119
import org .hibernate .internal .util .LazyValue ;
120
+ import org .hibernate .internal .util .MarkerObject ;
120
121
import org .hibernate .internal .util .StringHelper ;
121
122
import org .hibernate .internal .util .collections .ArrayHelper ;
122
- import org .hibernate .internal .util .collections .CollectionHelper ;
123
123
import org .hibernate .internal .util .collections .LockModeEnumMap ;
124
124
import org .hibernate .jdbc .Expectation ;
125
125
import org .hibernate .jdbc .TooManyRowsAffectedException ;
158
158
import org .hibernate .metamodel .mapping .AttributeMapping ;
159
159
import org .hibernate .metamodel .mapping .AttributeMappingsList ;
160
160
import org .hibernate .metamodel .mapping .AttributeMappingsMap ;
161
- import org .hibernate .metamodel .mapping .AttributeMetadata ;
162
161
import org .hibernate .metamodel .mapping .DiscriminatorConverter ;
163
162
import org .hibernate .metamodel .mapping .DiscriminatorType ;
164
163
import org .hibernate .metamodel .mapping .EmbeddableValuedModelPart ;
253
252
import org .hibernate .sql .ast .tree .predicate .ComparisonPredicate ;
254
253
import org .hibernate .sql .ast .tree .predicate .InListPredicate ;
255
254
import org .hibernate .sql .ast .tree .predicate .Junction ;
256
- import org .hibernate .sql .ast .tree .predicate .NegatedPredicate ;
257
255
import org .hibernate .sql .ast .tree .predicate .NullnessPredicate ;
258
256
import org .hibernate .sql .ast .tree .predicate .Predicate ;
259
257
import org .hibernate .sql .ast .tree .select .QuerySpec ;
313
311
import static org .hibernate .internal .util .collections .CollectionHelper .combine ;
314
312
import static org .hibernate .internal .util .collections .CollectionHelper .isNotEmpty ;
315
313
import static org .hibernate .internal .util .collections .CollectionHelper .setOfSize ;
314
+ import static org .hibernate .internal .util .collections .CollectionHelper .toSmallList ;
316
315
import static org .hibernate .loader .ast .internal .MultiKeyLoadHelper .supportsSqlArrayType ;
317
316
import static org .hibernate .metamodel .RepresentationMode .POJO ;
318
317
import static org .hibernate .persister .entity .DiscriminatorHelper .NOT_NULL_DISCRIMINATOR ;
@@ -905,27 +904,25 @@ else if ( entityMetamodel.isMutable() ) {
905
904
}
906
905
}
907
906
907
+ private static CacheLayout queryCacheLayout (CacheLayout entityQueryCacheLayout , SessionFactoryOptions options ) {
908
+ return entityQueryCacheLayout == null ? options .getQueryCacheLayout () : entityQueryCacheLayout ;
909
+ }
910
+
908
911
private boolean shouldUseShallowCacheLayout (CacheLayout entityQueryCacheLayout , SessionFactoryOptions options ) {
909
- final CacheLayout queryCacheLayout ;
910
- if ( entityQueryCacheLayout == null ) {
911
- queryCacheLayout = options .getQueryCacheLayout ();
912
- }
913
- else {
914
- queryCacheLayout = entityQueryCacheLayout ;
912
+ switch ( queryCacheLayout ( entityQueryCacheLayout , options ) ) {
913
+ case FULL :
914
+ return false ;
915
+ case AUTO :
916
+ return canUseReferenceCacheEntries ()
917
+ || canReadFromCache ();
918
+ default :
919
+ return true ;
915
920
}
916
- return queryCacheLayout == CacheLayout .SHALLOW || queryCacheLayout == CacheLayout .SHALLOW_WITH_DISCRIMINATOR
917
- || queryCacheLayout == CacheLayout .AUTO && ( canUseReferenceCacheEntries () || canReadFromCache () );
918
921
}
919
922
920
- private boolean shouldStoreDiscriminatorInShallowQueryCacheLayout (CacheLayout entityQueryCacheLayout , SessionFactoryOptions options ) {
921
- final CacheLayout queryCacheLayout ;
922
- if ( entityQueryCacheLayout == null ) {
923
- queryCacheLayout = options .getQueryCacheLayout ();
924
- }
925
- else {
926
- queryCacheLayout = entityQueryCacheLayout ;
927
- }
928
- return queryCacheLayout == CacheLayout .SHALLOW_WITH_DISCRIMINATOR ;
923
+ private static boolean shouldStoreDiscriminatorInShallowQueryCacheLayout (
924
+ CacheLayout entityQueryCacheLayout , SessionFactoryOptions options ) {
925
+ return queryCacheLayout ( entityQueryCacheLayout , options ) == CacheLayout .SHALLOW_WITH_DISCRIMINATOR ;
929
926
}
930
927
931
928
protected abstract String [] getSubclassTableNames ();
@@ -1170,10 +1167,10 @@ public boolean hasFilterForLoadByKey() {
1170
1167
1171
1168
@ Override
1172
1169
public Iterable <UniqueKeyEntry > uniqueKeyEntries () {
1173
- if ( this . uniqueKeyEntries == null ) {
1174
- this . uniqueKeyEntries = initUniqueKeyEntries ( this );
1170
+ if ( uniqueKeyEntries == null ) {
1171
+ uniqueKeyEntries = initUniqueKeyEntries ( this );
1175
1172
}
1176
- return this . uniqueKeyEntries ;
1173
+ return uniqueKeyEntries ;
1177
1174
}
1178
1175
1179
1176
private static List <UniqueKeyEntry > initUniqueKeyEntries (final AbstractEntityPersister aep ) {
@@ -1192,7 +1189,7 @@ private static List<UniqueKeyEntry> initUniqueKeyEntries(final AbstractEntityPer
1192
1189
}
1193
1190
}
1194
1191
}
1195
- return CollectionHelper . toSmallList ( uniqueKeys );
1192
+ return toSmallList ( uniqueKeys );
1196
1193
}
1197
1194
1198
1195
protected Map <String , SingleIdArrayLoadPlan > getLazyLoadPlanByFetchGroup () {
@@ -1230,7 +1227,7 @@ private SingleIdArrayLoadPlan createLazyLoadPlan(List<LazyAttributeDescriptor> f
1230
1227
return null ;
1231
1228
}
1232
1229
else {
1233
- JdbcParametersList .Builder jdbcParametersBuilder = JdbcParametersList .newBuilder ();
1230
+ final JdbcParametersList .Builder jdbcParametersBuilder = JdbcParametersList .newBuilder ();
1234
1231
final SelectStatement select = LoaderSelectBuilder .createSelect (
1235
1232
this ,
1236
1233
partsToSelect ,
@@ -1242,12 +1239,11 @@ private SingleIdArrayLoadPlan createLazyLoadPlan(List<LazyAttributeDescriptor> f
1242
1239
jdbcParametersBuilder ::add ,
1243
1240
factory
1244
1241
);
1245
- JdbcParametersList jdbcParameters = jdbcParametersBuilder .build ();
1246
1242
return new SingleIdArrayLoadPlan (
1247
1243
this ,
1248
1244
getIdentifierMapping (),
1249
1245
select ,
1250
- jdbcParameters ,
1246
+ jdbcParametersBuilder . build () ,
1251
1247
LockOptions .NONE ,
1252
1248
factory
1253
1249
);
@@ -2921,17 +2917,16 @@ private Predicate createDisciminatorPredicate(BasicType<?> discriminatorType, Ex
2921
2917
return new NullnessPredicate ( sqlExpression );
2922
2918
}
2923
2919
else if ( value == NOT_NULL_DISCRIMINATOR ) {
2924
- return new NegatedPredicate ( new NullnessPredicate ( sqlExpression ) );
2920
+ return new NullnessPredicate ( sqlExpression , true );
2925
2921
}
2926
2922
else {
2927
- final QueryLiteral < Object > literal = new QueryLiteral <>( value , discriminatorType );
2928
- return new ComparisonPredicate ( sqlExpression , ComparisonOperator . EQUAL , literal );
2923
+ return new ComparisonPredicate ( sqlExpression , ComparisonOperator . EQUAL ,
2924
+ new QueryLiteral <>( value , discriminatorType ) );
2929
2925
}
2930
2926
}
2931
2927
}
2932
2928
2933
2929
private Predicate createInListPredicate (BasicType <?> discriminatorType , Expression sqlExpression ) {
2934
- final List <Expression > values = new ArrayList <>( fullDiscriminatorValues .length );
2935
2930
boolean hasNull = false , hasNonNull = false ;
2936
2931
for ( Object discriminatorValue : fullDiscriminatorValues ) {
2937
2932
if ( discriminatorValue == NULL_DISCRIMINATOR ) {
@@ -2940,43 +2935,48 @@ private Predicate createInListPredicate(BasicType<?> discriminatorType, Expressi
2940
2935
else if ( discriminatorValue == NOT_NULL_DISCRIMINATOR ) {
2941
2936
hasNonNull = true ;
2942
2937
}
2943
- else {
2944
- values .add ( new QueryLiteral <>( discriminatorValue , discriminatorType ) );
2945
- }
2946
2938
}
2947
- final Predicate predicate = new InListPredicate ( sqlExpression , values );
2948
- if ( hasNull || hasNonNull ) {
2939
+ if ( hasNull && hasNonNull ) {
2940
+ // This means we need to select all rows,
2941
+ // and so we don't need a predicate at all
2942
+ // Just return an empty Junction
2943
+ return new Junction ( Junction .Nature .DISJUNCTION );
2944
+ }
2945
+ else if ( hasNonNull ) {
2946
+ // we need every row with a non-null discriminator
2947
+ return new NullnessPredicate ( sqlExpression , true );
2948
+ }
2949
+ else if ( hasNull ) {
2949
2950
final Junction junction = new Junction ( Junction .Nature .DISJUNCTION );
2951
+ junction .add ( new NullnessPredicate ( sqlExpression ) );
2952
+ junction .add ( discriminatorValuesPredicate ( discriminatorType , sqlExpression ) );
2953
+ return junction ;
2954
+ }
2955
+ else {
2956
+ return discriminatorValuesPredicate ( discriminatorType , sqlExpression );
2957
+ }
2958
+ }
2950
2959
2951
- if ( hasNull && hasNonNull ) {
2952
- // This means we need to select everything, we don't need a predicate at all
2953
- // Return an empty Junction
2954
- return junction ;
2955
- }
2956
-
2957
- if ( hasNonNull ) {
2958
- return new NullnessPredicate ( sqlExpression , true );
2959
- }
2960
- else if ( hasNull ) {
2961
- junction .add ( new NullnessPredicate ( sqlExpression ) );
2960
+ private InListPredicate discriminatorValuesPredicate (BasicType <?> discriminatorType , Expression sqlExpression ) {
2961
+ final List <Expression > values = new ArrayList <>( fullDiscriminatorValues .length );
2962
+ for ( Object discriminatorValue : fullDiscriminatorValues ) {
2963
+ if ( !(discriminatorValue instanceof MarkerObject ) ) {
2964
+ values .add ( new QueryLiteral <>( discriminatorValue , discriminatorType ) );
2962
2965
}
2963
-
2964
- junction .add ( predicate );
2965
- return junction ;
2966
2966
}
2967
- return predicate ;
2967
+ return new InListPredicate ( sqlExpression , values ) ;
2968
2968
}
2969
2969
2970
2970
protected String getPrunedDiscriminatorPredicate (
2971
2971
Map <String , EntityNameUse > entityNameUses ,
2972
2972
MappingMetamodelImplementor mappingMetamodel ,
2973
2973
String alias ) {
2974
- final InFragment frag = new InFragment ();
2974
+ final InFragment fragment = new InFragment ();
2975
2975
if ( isDiscriminatorFormula () ) {
2976
- frag .setFormula ( alias , getDiscriminatorFormulaTemplate () );
2976
+ fragment .setFormula ( alias , getDiscriminatorFormulaTemplate () );
2977
2977
}
2978
2978
else {
2979
- frag .setColumn ( alias , getDiscriminatorColumnName () );
2979
+ fragment .setColumn ( alias , getDiscriminatorColumnName () );
2980
2980
}
2981
2981
boolean containsNotNull = false ;
2982
2982
for ( Map .Entry <String , EntityNameUse > entry : entityNameUses .entrySet () ) {
@@ -2991,11 +2991,12 @@ protected String getPrunedDiscriminatorPredicate(
2991
2991
// as the query will contain a filter for that already anyway
2992
2992
if ( !persister .isAbstract () && ( this == persister || !isTypeOrSuperType ( persister ) ) ) {
2993
2993
containsNotNull = containsNotNull || InFragment .NOT_NULL .equals ( persister .getDiscriminatorSQLValue () );
2994
- frag .addValue ( persister .getDiscriminatorSQLValue () );
2994
+ fragment .addValue ( persister .getDiscriminatorSQLValue () );
2995
2995
}
2996
2996
}
2997
- final List <String > discriminatorSQLValues = Arrays .asList ( ( (AbstractEntityPersister ) getRootEntityDescriptor () ).fullDiscriminatorSQLValues );
2998
- if ( frag .getValues ().size () == discriminatorSQLValues .size () ) {
2997
+ final AbstractEntityPersister rootEntityDescriptor = (AbstractEntityPersister ) getRootEntityDescriptor ();
2998
+ final List <String > discriminatorSQLValues = Arrays .asList ( rootEntityDescriptor .fullDiscriminatorSQLValues );
2999
+ if ( fragment .getValues ().size () == discriminatorSQLValues .size () ) {
2999
3000
// Nothing to prune if we filter for all subtypes
3000
3001
return null ;
3001
3002
}
@@ -3010,7 +3011,7 @@ protected String getPrunedDiscriminatorPredicate(
3010
3011
}
3011
3012
final List <String > actualDiscriminatorSQLValues = new ArrayList <>( discriminatorSQLValues .size () );
3012
3013
for ( String value : discriminatorSQLValues ) {
3013
- if ( !frag .getValues ().contains ( value ) && !InFragment .NULL .equals ( value ) ) {
3014
+ if ( !fragment .getValues ().contains ( value ) && !InFragment .NULL .equals ( value ) ) {
3014
3015
actualDiscriminatorSQLValues .add ( value );
3015
3016
}
3016
3017
}
@@ -3021,11 +3022,11 @@ protected String getPrunedDiscriminatorPredicate(
3021
3022
sb .append ( ") and " );
3022
3023
}
3023
3024
sb .append ( lhs ).append ( " is not null" );
3024
- frag .getValues ().remove ( InFragment .NOT_NULL );
3025
- return frag .toFragmentString () + sb ;
3025
+ fragment .getValues ().remove ( InFragment .NOT_NULL );
3026
+ return fragment .toFragmentString () + sb ;
3026
3027
}
3027
3028
else {
3028
- return frag .toFragmentString ();
3029
+ return fragment .toFragmentString ();
3029
3030
}
3030
3031
}
3031
3032
0 commit comments