From 1ede36212bf0b02198396671b747d4459b0d0335 Mon Sep 17 00:00:00 2001 From: William Burns Date: Tue, 18 Jan 2022 11:43:28 -0500 Subject: [PATCH] HHH-15037 Criteria Update/Delete with vararg parameters throws NullPointerException --- .../AbstractSqmRestrictedDmlStatement.java | 22 +++++++++++-------- .../criteria/ManipulationCriteriaTest.java | 18 ++++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/AbstractSqmRestrictedDmlStatement.java b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/AbstractSqmRestrictedDmlStatement.java index 766ab46098..fe3d895863 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/AbstractSqmRestrictedDmlStatement.java +++ b/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/AbstractSqmRestrictedDmlStatement.java @@ -62,11 +62,7 @@ public abstract class AbstractSqmRestrictedDmlStatement extends AbstractSqmDm return; } - if ( whereClause == null ) { - whereClause = new SqmWhereClause( nodeBuilder() ); - } - - whereClause.applyPredicate( predicate ); + initAndGetWhereClause().applyPredicate( predicate ); } public void setWhereClause(SqmWhereClause whereClause) { @@ -79,13 +75,21 @@ public abstract class AbstractSqmRestrictedDmlStatement extends AbstractSqmDm } protected void setWhere(Expression restriction) { - applyPredicate( null ); - applyPredicate( (SqmPredicate) restriction ); + // Replaces the current predicate if one is present + initAndGetWhereClause().setPredicate( (SqmPredicate) restriction ); + } + + protected SqmWhereClause initAndGetWhereClause() { + if ( whereClause == null ) { + whereClause = new SqmWhereClause( nodeBuilder() ); + } + return whereClause; } protected void setWhere(Predicate... restrictions) { - applyPredicate( null ); - final SqmWhereClause whereClause = getWhereClause(); + final SqmWhereClause whereClause = initAndGetWhereClause(); + // Clear the current predicate if one is present + whereClause.setPredicate(null); for ( Predicate restriction : restrictions ) { whereClause.applyPredicate( (SqmPredicate) restriction ); } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/ManipulationCriteriaTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/ManipulationCriteriaTest.java index b8a06b35eb..31c99a1baa 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/ManipulationCriteriaTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/criteria/ManipulationCriteriaTest.java @@ -65,7 +65,7 @@ public class ManipulationCriteriaTest extends AbstractMetamodelSpecificTest { { CriteriaUpdate updateCriteria = builder.createCriteriaUpdate( Customer.class ); Root root = updateCriteria.from( Customer.class ); - updateCriteria.set( Customer_.name, "Acme" ); + updateCriteria.set( Customer_.age, 23 ); updateCriteria.where( builder.equal( root.get( Customer_.name ), @@ -75,6 +75,22 @@ public class ManipulationCriteriaTest extends AbstractMetamodelSpecificTest { em.createQuery( updateCriteria ).executeUpdate(); } + { + CriteriaDelete deleteCriteria = builder.createCriteriaDelete( Customer.class ); + Root root = deleteCriteria.from( Customer.class ); + deleteCriteria.where( + builder.equal( + root.get( Customer_.name ), + "Acme" + ), + builder.equal( + root.get(Customer_.age), + 23 + ) + ); + em.createQuery( deleteCriteria ).executeUpdate(); + } + em.getTransaction().commit(); em.close(); }