Skip to content

Commit e1ded5d

Browse files
committed
HHH-18556 Expressions.nullExpresion() in querydsl result in NPE in SqmExpressible
1 parent 8efcf26 commit e1ded5d

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

hibernate-core/src/main/java/org/hibernate/query/internal/QueryParameterBindingImpl.java

+18-10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.hibernate.query.spi.QueryParameterBinding;
2121
import org.hibernate.query.spi.QueryParameterBindingValidator;
2222
import org.hibernate.query.sqm.SqmExpressible;
23+
import org.hibernate.query.sqm.tree.expression.NullSqmExpressible;
2324
import org.hibernate.type.descriptor.java.JavaType;
2425
import org.hibernate.type.descriptor.java.JavaTypeHelper;
2526
import org.hibernate.type.spi.TypeConfiguration;
@@ -113,10 +114,10 @@ public void setBindValue(T value, boolean resolveJdbcTypeIfNecessary) {
113114
final Object coerced;
114115
if ( ! sessionFactory.getSessionFactoryOptions().getJpaCompliance().isLoadByIdComplianceEnabled() ) {
115116
try {
116-
if ( bindType != null ) {
117+
if ( canValueBeCoerced( bindType) ) {
117118
coerced = coerce( value, bindType );
118119
}
119-
else if ( queryParameter.getHibernateType() != null ) {
120+
else if ( canValueBeCoerced( queryParameter.getHibernateType() ) ) {
120121
coerced = coerce( value, queryParameter.getHibernateType() );
121122
}
122123
else {
@@ -190,7 +191,7 @@ private boolean isRegisteredAsBasicType(Class<?> valueClass) {
190191
private void bindValue(Object value) {
191192
isBound = true;
192193
bindValue = value;
193-
if ( bindType == null && value != null ) {
194+
if ( canBindValueBeSet( value, bindType ) ) {
194195
bindType = sessionFactory.getMappingMetamodel().resolveParameterBindType( value );
195196
}
196197
}
@@ -206,10 +207,10 @@ public void setBindValue(T value, BindableType<T> clarifiedType) {
206207
}
207208

208209
final Object coerced;
209-
if ( bindType != null ) {
210+
if ( canValueBeCoerced( bindType ) ) {
210211
coerced = coerce( value, bindType );
211212
}
212-
else if ( queryParameter.getHibernateType() != null ) {
213+
else if ( canValueBeCoerced( queryParameter.getHibernateType() ) ) {
213214
coerced = coerce( value, queryParameter.getHibernateType() );
214215
}
215216
else {
@@ -233,7 +234,7 @@ public void setBindValue(T value, TemporalType temporalTypePrecision) {
233234

234235
final Object coerced;
235236
if ( ! sessionFactory.getSessionFactoryOptions().getJpaCompliance().isLoadByIdComplianceEnabled() ) {
236-
if ( bindType != null ) {
237+
if (canValueBeCoerced( bindType ) ) {
237238
try {
238239
coerced = coerce( value, bindType );
239240
}
@@ -249,7 +250,7 @@ public void setBindValue(T value, TemporalType temporalTypePrecision) {
249250
);
250251
}
251252
}
252-
else if ( queryParameter.getHibernateType() != null ) {
253+
else if ( canValueBeCoerced( queryParameter.getHibernateType() ) ) {
253254
coerced = coerce( value, queryParameter.getHibernateType() );
254255
}
255256
else {
@@ -299,10 +300,9 @@ public void setBindValues(Collection<? extends T> values) {
299300
value = iterator.next();
300301
}
301302

302-
if ( bindType == null && value != null ) {
303-
this.bindType = sessionFactory.getMappingMetamodel().resolveParameterBindType( value );
303+
if ( canBindValueBeSet( value, bindType ) ) {
304+
bindType = sessionFactory.getMappingMetamodel().resolveParameterBindType( value );
304305
}
305-
306306
}
307307

308308
@Override
@@ -378,4 +378,12 @@ private void validate(Object value, TemporalType clarifiedTemporalType) {
378378
public TypeConfiguration getTypeConfiguration() {
379379
return sessionFactory.getTypeConfiguration();
380380
}
381+
382+
private static boolean canValueBeCoerced(BindableType<?> bindType) {
383+
return bindType != null && !( bindType instanceof NullSqmExpressible );
384+
}
385+
386+
private static boolean canBindValueBeSet(Object value, BindableType<?> bindType) {
387+
return value != null && ( bindType == null || bindType instanceof NullSqmExpressible );
388+
}
381389
}

hibernate-core/src/main/java/org/hibernate/query/spi/AbstractCommonQueryContract.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.hibernate.query.criteria.JpaExpression;
4646
import org.hibernate.query.internal.QueryOptionsImpl;
4747
import org.hibernate.query.sqm.SqmExpressible;
48+
import org.hibernate.query.sqm.tree.expression.NullSqmExpressible;
4849
import org.hibernate.query.sqm.tree.expression.SqmLiteral;
4950
import org.hibernate.query.sqm.tree.expression.SqmParameter;
5051
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
@@ -906,7 +907,7 @@ public CommonQueryContract setParameter(int position, Object value) {
906907
final QueryParameter<Object> param = binding.getQueryParameter();
907908
if ( param.allowsMultiValuedBinding() ) {
908909
final BindableType<?> hibernateType = param.getHibernateType();
909-
if ( hibernateType == null || isInstance( hibernateType, value ) ) {
910+
if ( hibernateType == null || hibernateType instanceof NullSqmExpressible || isInstance( hibernateType, value ) ) {
910911
if ( value instanceof Collection && !isRegisteredAsBasicType( value.getClass() ) ) {
911912
//noinspection rawtypes
912913
return setParameterList( position, (Collection) value );

0 commit comments

Comments
 (0)