From 9fba739bc2327a2402262c29cdcb6fd4f4477834 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 13 Jan 2022 18:34:27 +0100 Subject: [PATCH] Throw IllegalArgumentException for null Criteria literal value --- .../sqm/internal/SqmCriteriaNodeBuilder.java | 3 ++ .../jpa/compliance/CriteriaLiteralValue.java | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/CriteriaLiteralValue.java diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java index 03b13e29b7..1996a2a3cd 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/internal/SqmCriteriaNodeBuilder.java @@ -914,6 +914,9 @@ public class SqmCriteriaNodeBuilder implements NodeBuilder, SqmCreationContext, @Override public SqmLiteral literal(T value) { if ( value == null ) { + if ( jpaComplianceEnabled ) { + throw new IllegalArgumentException( "literal value cannot be null" ); + } return new SqmLiteralNull<>( this ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/CriteriaLiteralValue.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/CriteriaLiteralValue.java new file mode 100644 index 0000000000..7e186cff16 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/compliance/CriteriaLiteralValue.java @@ -0,0 +1,31 @@ +package org.hibernate.orm.test.jpa.compliance; + +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.criteria.CriteriaBuilder; + +import static org.junit.jupiter.api.Assertions.fail; + +@Jpa( + properties = @Setting(name = AvailableSettings.JPA_QUERY_COMPLIANCE, value = "true") +) +public class CriteriaLiteralValue { + + @Test + public void testLiteralValueCannotBeNull(EntityManagerFactoryScope scope) { + + final CriteriaBuilder builder = scope.getEntityManagerFactory().getCriteriaBuilder(); + try { + builder.literal(null); + fail( "TCK expects an IllegalArgumentException" ); + } catch (IllegalArgumentException iae) { +// //expected by TCK + } + } + +}